xref: /openbmc/qemu/ui/vnc.c (revision 7d964c9d2fc614d2baa788b9e77019b32151f162)
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
38999342a0SCorentin Chary static const struct timeval VNC_REFRESH_STATS = { 0, 500000 };
39999342a0SCorentin Chary static const struct timeval VNC_REFRESH_LOSSY = { 2, 0 };
403e230dd2SCorentin Chary 
413e230dd2SCorentin Chary #include "vnc_keysym.h"
423e230dd2SCorentin Chary #include "d3des.h"
433e230dd2SCorentin Chary 
443e230dd2SCorentin Chary #define count_bits(c, v) { \
453e230dd2SCorentin Chary     for (c = 0; v; v >>= 1) \
463e230dd2SCorentin Chary     { \
473e230dd2SCorentin Chary         c += v & 1; \
483e230dd2SCorentin Chary     } \
493e230dd2SCorentin Chary }
503e230dd2SCorentin Chary 
513e230dd2SCorentin Chary static VncDisplay *vnc_display; /* needed for info vnc */
523e230dd2SCorentin Chary static DisplayChangeListener *dcl;
533e230dd2SCorentin Chary 
543e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs);
553e230dd2SCorentin Chary 
563e230dd2SCorentin Chary static char *addr_to_string(const char *format,
573e230dd2SCorentin Chary                             struct sockaddr_storage *sa,
583e230dd2SCorentin Chary                             socklen_t salen) {
593e230dd2SCorentin Chary     char *addr;
603e230dd2SCorentin Chary     char host[NI_MAXHOST];
613e230dd2SCorentin Chary     char serv[NI_MAXSERV];
623e230dd2SCorentin Chary     int err;
633e230dd2SCorentin Chary     size_t addrlen;
643e230dd2SCorentin Chary 
653e230dd2SCorentin Chary     if ((err = getnameinfo((struct sockaddr *)sa, salen,
663e230dd2SCorentin Chary                            host, sizeof(host),
673e230dd2SCorentin Chary                            serv, sizeof(serv),
683e230dd2SCorentin Chary                            NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
693e230dd2SCorentin Chary         VNC_DEBUG("Cannot resolve address %d: %s\n",
703e230dd2SCorentin Chary                   err, gai_strerror(err));
713e230dd2SCorentin Chary         return NULL;
723e230dd2SCorentin Chary     }
733e230dd2SCorentin Chary 
743e230dd2SCorentin Chary     /* Enough for the existing format + the 2 vars we're
753e230dd2SCorentin Chary      * substituting in. */
763e230dd2SCorentin Chary     addrlen = strlen(format) + strlen(host) + strlen(serv);
773e230dd2SCorentin Chary     addr = qemu_malloc(addrlen + 1);
783e230dd2SCorentin Chary     snprintf(addr, addrlen, format, host, serv);
793e230dd2SCorentin Chary     addr[addrlen] = '\0';
803e230dd2SCorentin Chary 
813e230dd2SCorentin Chary     return addr;
823e230dd2SCorentin Chary }
833e230dd2SCorentin Chary 
843e230dd2SCorentin Chary 
853e230dd2SCorentin Chary char *vnc_socket_local_addr(const char *format, int fd) {
863e230dd2SCorentin Chary     struct sockaddr_storage sa;
873e230dd2SCorentin Chary     socklen_t salen;
883e230dd2SCorentin Chary 
893e230dd2SCorentin Chary     salen = sizeof(sa);
903e230dd2SCorentin Chary     if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0)
913e230dd2SCorentin Chary         return NULL;
923e230dd2SCorentin Chary 
933e230dd2SCorentin Chary     return addr_to_string(format, &sa, salen);
943e230dd2SCorentin Chary }
953e230dd2SCorentin Chary 
963e230dd2SCorentin Chary char *vnc_socket_remote_addr(const char *format, int fd) {
973e230dd2SCorentin Chary     struct sockaddr_storage sa;
983e230dd2SCorentin Chary     socklen_t salen;
993e230dd2SCorentin Chary 
1003e230dd2SCorentin Chary     salen = sizeof(sa);
1013e230dd2SCorentin Chary     if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0)
1023e230dd2SCorentin Chary         return NULL;
1033e230dd2SCorentin Chary 
1043e230dd2SCorentin Chary     return addr_to_string(format, &sa, salen);
1053e230dd2SCorentin Chary }
1063e230dd2SCorentin Chary 
1073e230dd2SCorentin Chary static int put_addr_qdict(QDict *qdict, struct sockaddr_storage *sa,
1083e230dd2SCorentin Chary                           socklen_t salen)
1093e230dd2SCorentin Chary {
1103e230dd2SCorentin Chary     char host[NI_MAXHOST];
1113e230dd2SCorentin Chary     char serv[NI_MAXSERV];
1123e230dd2SCorentin Chary     int err;
1133e230dd2SCorentin Chary 
1143e230dd2SCorentin Chary     if ((err = getnameinfo((struct sockaddr *)sa, salen,
1153e230dd2SCorentin Chary                            host, sizeof(host),
1163e230dd2SCorentin Chary                            serv, sizeof(serv),
1173e230dd2SCorentin Chary                            NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
1183e230dd2SCorentin Chary         VNC_DEBUG("Cannot resolve address %d: %s\n",
1193e230dd2SCorentin Chary                   err, gai_strerror(err));
1203e230dd2SCorentin Chary         return -1;
1213e230dd2SCorentin Chary     }
1223e230dd2SCorentin Chary 
1233e230dd2SCorentin Chary     qdict_put(qdict, "host", qstring_from_str(host));
1243e230dd2SCorentin Chary     qdict_put(qdict, "service", qstring_from_str(serv));
1253e230dd2SCorentin Chary     qdict_put(qdict, "family",qstring_from_str(inet_strfamily(sa->ss_family)));
1263e230dd2SCorentin Chary 
1273e230dd2SCorentin Chary     return 0;
1283e230dd2SCorentin Chary }
1293e230dd2SCorentin Chary 
1303e230dd2SCorentin Chary static int vnc_server_addr_put(QDict *qdict, int fd)
1313e230dd2SCorentin Chary {
1323e230dd2SCorentin Chary     struct sockaddr_storage sa;
1333e230dd2SCorentin Chary     socklen_t salen;
1343e230dd2SCorentin Chary 
1353e230dd2SCorentin Chary     salen = sizeof(sa);
1363e230dd2SCorentin Chary     if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) {
1373e230dd2SCorentin Chary         return -1;
1383e230dd2SCorentin Chary     }
1393e230dd2SCorentin Chary 
1403e230dd2SCorentin Chary     return put_addr_qdict(qdict, &sa, salen);
1413e230dd2SCorentin Chary }
1423e230dd2SCorentin Chary 
1433e230dd2SCorentin Chary static int vnc_qdict_remote_addr(QDict *qdict, int fd)
1443e230dd2SCorentin Chary {
1453e230dd2SCorentin Chary     struct sockaddr_storage sa;
1463e230dd2SCorentin Chary     socklen_t salen;
1473e230dd2SCorentin Chary 
1483e230dd2SCorentin Chary     salen = sizeof(sa);
1493e230dd2SCorentin Chary     if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) {
1503e230dd2SCorentin Chary         return -1;
1513e230dd2SCorentin Chary     }
1523e230dd2SCorentin Chary 
1533e230dd2SCorentin Chary     return put_addr_qdict(qdict, &sa, salen);
1543e230dd2SCorentin Chary }
1553e230dd2SCorentin Chary 
1563e230dd2SCorentin Chary static const char *vnc_auth_name(VncDisplay *vd) {
1573e230dd2SCorentin Chary     switch (vd->auth) {
1583e230dd2SCorentin Chary     case VNC_AUTH_INVALID:
1593e230dd2SCorentin Chary         return "invalid";
1603e230dd2SCorentin Chary     case VNC_AUTH_NONE:
1613e230dd2SCorentin Chary         return "none";
1623e230dd2SCorentin Chary     case VNC_AUTH_VNC:
1633e230dd2SCorentin Chary         return "vnc";
1643e230dd2SCorentin Chary     case VNC_AUTH_RA2:
1653e230dd2SCorentin Chary         return "ra2";
1663e230dd2SCorentin Chary     case VNC_AUTH_RA2NE:
1673e230dd2SCorentin Chary         return "ra2ne";
1683e230dd2SCorentin Chary     case VNC_AUTH_TIGHT:
1693e230dd2SCorentin Chary         return "tight";
1703e230dd2SCorentin Chary     case VNC_AUTH_ULTRA:
1713e230dd2SCorentin Chary         return "ultra";
1723e230dd2SCorentin Chary     case VNC_AUTH_TLS:
1733e230dd2SCorentin Chary         return "tls";
1743e230dd2SCorentin Chary     case VNC_AUTH_VENCRYPT:
1753e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
1763e230dd2SCorentin Chary         switch (vd->subauth) {
1773e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_PLAIN:
1783e230dd2SCorentin Chary             return "vencrypt+plain";
1793e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSNONE:
1803e230dd2SCorentin Chary             return "vencrypt+tls+none";
1813e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSVNC:
1823e230dd2SCorentin Chary             return "vencrypt+tls+vnc";
1833e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSPLAIN:
1843e230dd2SCorentin Chary             return "vencrypt+tls+plain";
1853e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509NONE:
1863e230dd2SCorentin Chary             return "vencrypt+x509+none";
1873e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509VNC:
1883e230dd2SCorentin Chary             return "vencrypt+x509+vnc";
1893e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509PLAIN:
1903e230dd2SCorentin Chary             return "vencrypt+x509+plain";
1913e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSSASL:
1923e230dd2SCorentin Chary             return "vencrypt+tls+sasl";
1933e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509SASL:
1943e230dd2SCorentin Chary             return "vencrypt+x509+sasl";
1953e230dd2SCorentin Chary         default:
1963e230dd2SCorentin Chary             return "vencrypt";
1973e230dd2SCorentin Chary         }
1983e230dd2SCorentin Chary #else
1993e230dd2SCorentin Chary         return "vencrypt";
2003e230dd2SCorentin Chary #endif
2013e230dd2SCorentin Chary     case VNC_AUTH_SASL:
2023e230dd2SCorentin Chary         return "sasl";
2033e230dd2SCorentin Chary     }
2043e230dd2SCorentin Chary     return "unknown";
2053e230dd2SCorentin Chary }
2063e230dd2SCorentin Chary 
2073e230dd2SCorentin Chary static int vnc_server_info_put(QDict *qdict)
2083e230dd2SCorentin Chary {
2093e230dd2SCorentin Chary     if (vnc_server_addr_put(qdict, vnc_display->lsock) < 0) {
2103e230dd2SCorentin Chary         return -1;
2113e230dd2SCorentin Chary     }
2123e230dd2SCorentin Chary 
2133e230dd2SCorentin Chary     qdict_put(qdict, "auth", qstring_from_str(vnc_auth_name(vnc_display)));
2143e230dd2SCorentin Chary     return 0;
2153e230dd2SCorentin Chary }
2163e230dd2SCorentin Chary 
2173e230dd2SCorentin Chary static void vnc_client_cache_auth(VncState *client)
2183e230dd2SCorentin Chary {
2192ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
2203e230dd2SCorentin Chary     QDict *qdict;
2212ded6ad7SBlue Swirl #endif
2223e230dd2SCorentin Chary 
2233e230dd2SCorentin Chary     if (!client->info) {
2243e230dd2SCorentin Chary         return;
2253e230dd2SCorentin Chary     }
2263e230dd2SCorentin Chary 
2272ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
2283e230dd2SCorentin Chary     qdict = qobject_to_qdict(client->info);
2292ded6ad7SBlue Swirl #endif
2303e230dd2SCorentin Chary 
2313e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
2323e230dd2SCorentin Chary     if (client->tls.session &&
2333e230dd2SCorentin Chary         client->tls.dname) {
2343e230dd2SCorentin Chary         qdict_put(qdict, "x509_dname", qstring_from_str(client->tls.dname));
2353e230dd2SCorentin Chary     }
2363e230dd2SCorentin Chary #endif
2373e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
2383e230dd2SCorentin Chary     if (client->sasl.conn &&
2393e230dd2SCorentin Chary         client->sasl.username) {
2403e230dd2SCorentin Chary         qdict_put(qdict, "sasl_username",
2413e230dd2SCorentin Chary                   qstring_from_str(client->sasl.username));
2423e230dd2SCorentin Chary     }
2433e230dd2SCorentin Chary #endif
2443e230dd2SCorentin Chary }
2453e230dd2SCorentin Chary 
2463e230dd2SCorentin Chary static void vnc_client_cache_addr(VncState *client)
2473e230dd2SCorentin Chary {
2483e230dd2SCorentin Chary     QDict *qdict;
2493e230dd2SCorentin Chary 
2503e230dd2SCorentin Chary     qdict = qdict_new();
2513e230dd2SCorentin Chary     if (vnc_qdict_remote_addr(qdict, client->csock) < 0) {
2523e230dd2SCorentin Chary         QDECREF(qdict);
2533e230dd2SCorentin Chary         /* XXX: how to report the error? */
2543e230dd2SCorentin Chary         return;
2553e230dd2SCorentin Chary     }
2563e230dd2SCorentin Chary 
2573e230dd2SCorentin Chary     client->info = QOBJECT(qdict);
2583e230dd2SCorentin Chary }
2593e230dd2SCorentin Chary 
2603e230dd2SCorentin Chary static void vnc_qmp_event(VncState *vs, MonitorEvent event)
2613e230dd2SCorentin Chary {
2623e230dd2SCorentin Chary     QDict *server;
2633e230dd2SCorentin Chary     QObject *data;
2643e230dd2SCorentin Chary 
2653e230dd2SCorentin Chary     if (!vs->info) {
2663e230dd2SCorentin Chary         return;
2673e230dd2SCorentin Chary     }
2683e230dd2SCorentin Chary 
2693e230dd2SCorentin Chary     server = qdict_new();
2703e230dd2SCorentin Chary     if (vnc_server_info_put(server) < 0) {
2713e230dd2SCorentin Chary         QDECREF(server);
2723e230dd2SCorentin Chary         return;
2733e230dd2SCorentin Chary     }
2743e230dd2SCorentin Chary 
2753e230dd2SCorentin Chary     data = qobject_from_jsonf("{ 'client': %p, 'server': %p }",
2763e230dd2SCorentin Chary                               vs->info, QOBJECT(server));
2773e230dd2SCorentin Chary 
2783e230dd2SCorentin Chary     monitor_protocol_event(event, data);
2793e230dd2SCorentin Chary 
2803e230dd2SCorentin Chary     qobject_incref(vs->info);
2813e230dd2SCorentin Chary     qobject_decref(data);
2823e230dd2SCorentin Chary }
2833e230dd2SCorentin Chary 
2843e230dd2SCorentin Chary static void info_vnc_iter(QObject *obj, void *opaque)
2853e230dd2SCorentin Chary {
2863e230dd2SCorentin Chary     QDict *client;
2873e230dd2SCorentin Chary     Monitor *mon = opaque;
2883e230dd2SCorentin Chary 
2893e230dd2SCorentin Chary     client = qobject_to_qdict(obj);
2903e230dd2SCorentin Chary     monitor_printf(mon, "Client:\n");
2913e230dd2SCorentin Chary     monitor_printf(mon, "     address: %s:%s\n",
2923e230dd2SCorentin Chary                    qdict_get_str(client, "host"),
2933e230dd2SCorentin Chary                    qdict_get_str(client, "service"));
2943e230dd2SCorentin Chary 
2953e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
2963e230dd2SCorentin Chary     monitor_printf(mon, "  x509_dname: %s\n",
2973e230dd2SCorentin Chary         qdict_haskey(client, "x509_dname") ?
2983e230dd2SCorentin Chary         qdict_get_str(client, "x509_dname") : "none");
2993e230dd2SCorentin Chary #endif
3003e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
3013e230dd2SCorentin Chary     monitor_printf(mon, "    username: %s\n",
3023e230dd2SCorentin Chary         qdict_haskey(client, "sasl_username") ?
3033e230dd2SCorentin Chary         qdict_get_str(client, "sasl_username") : "none");
3043e230dd2SCorentin Chary #endif
3053e230dd2SCorentin Chary }
3063e230dd2SCorentin Chary 
3073e230dd2SCorentin Chary void do_info_vnc_print(Monitor *mon, const QObject *data)
3083e230dd2SCorentin Chary {
3093e230dd2SCorentin Chary     QDict *server;
3103e230dd2SCorentin Chary     QList *clients;
3113e230dd2SCorentin Chary 
3123e230dd2SCorentin Chary     server = qobject_to_qdict(data);
3133e230dd2SCorentin Chary     if (qdict_get_bool(server, "enabled") == 0) {
3143e230dd2SCorentin Chary         monitor_printf(mon, "Server: disabled\n");
3153e230dd2SCorentin Chary         return;
3163e230dd2SCorentin Chary     }
3173e230dd2SCorentin Chary 
3183e230dd2SCorentin Chary     monitor_printf(mon, "Server:\n");
3193e230dd2SCorentin Chary     monitor_printf(mon, "     address: %s:%s\n",
3203e230dd2SCorentin Chary                    qdict_get_str(server, "host"),
3213e230dd2SCorentin Chary                    qdict_get_str(server, "service"));
3223e230dd2SCorentin Chary     monitor_printf(mon, "        auth: %s\n", qdict_get_str(server, "auth"));
3233e230dd2SCorentin Chary 
3243e230dd2SCorentin Chary     clients = qdict_get_qlist(server, "clients");
3253e230dd2SCorentin Chary     if (qlist_empty(clients)) {
3263e230dd2SCorentin Chary         monitor_printf(mon, "Client: none\n");
3273e230dd2SCorentin Chary     } else {
3283e230dd2SCorentin Chary         qlist_iter(clients, info_vnc_iter, mon);
3293e230dd2SCorentin Chary     }
3303e230dd2SCorentin Chary }
3313e230dd2SCorentin Chary 
3323e230dd2SCorentin Chary void do_info_vnc(Monitor *mon, QObject **ret_data)
3333e230dd2SCorentin Chary {
3343e230dd2SCorentin Chary     if (vnc_display == NULL || vnc_display->display == NULL) {
3353e230dd2SCorentin Chary         *ret_data = qobject_from_jsonf("{ 'enabled': false }");
3363e230dd2SCorentin Chary     } else {
3373e230dd2SCorentin Chary         QList *clist;
3383e230dd2SCorentin Chary         VncState *client;
3393e230dd2SCorentin Chary 
3403e230dd2SCorentin Chary         clist = qlist_new();
3413e230dd2SCorentin Chary         QTAILQ_FOREACH(client, &vnc_display->clients, next) {
3423e230dd2SCorentin Chary             if (client->info) {
3433e230dd2SCorentin Chary                 /* incref so that it's not freed by upper layers */
3443e230dd2SCorentin Chary                 qobject_incref(client->info);
3453e230dd2SCorentin Chary                 qlist_append_obj(clist, client->info);
3463e230dd2SCorentin Chary             }
3473e230dd2SCorentin Chary         }
3483e230dd2SCorentin Chary 
3493e230dd2SCorentin Chary         *ret_data = qobject_from_jsonf("{ 'enabled': true, 'clients': %p }",
3503e230dd2SCorentin Chary                                        QOBJECT(clist));
3513e230dd2SCorentin Chary         assert(*ret_data != NULL);
3523e230dd2SCorentin Chary 
3533e230dd2SCorentin Chary         if (vnc_server_info_put(qobject_to_qdict(*ret_data)) < 0) {
3543e230dd2SCorentin Chary             qobject_decref(*ret_data);
3553e230dd2SCorentin Chary             *ret_data = NULL;
3563e230dd2SCorentin Chary         }
3573e230dd2SCorentin Chary     }
3583e230dd2SCorentin Chary }
3593e230dd2SCorentin Chary 
3603e230dd2SCorentin Chary /* TODO
3613e230dd2SCorentin Chary    1) Get the queue working for IO.
3623e230dd2SCorentin Chary    2) there is some weirdness when using the -S option (the screen is grey
3633e230dd2SCorentin Chary       and not totally invalidated
3643e230dd2SCorentin Chary    3) resolutions > 1024
3653e230dd2SCorentin Chary */
3663e230dd2SCorentin Chary 
3673e230dd2SCorentin Chary static int vnc_update_client(VncState *vs, int has_dirty);
368bd023f95SCorentin Chary static int vnc_update_client_sync(VncState *vs, int has_dirty);
3693e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs);
3703e230dd2SCorentin Chary static void vnc_disconnect_finish(VncState *vs);
3713e230dd2SCorentin Chary static void vnc_init_timer(VncDisplay *vd);
3723e230dd2SCorentin Chary static void vnc_remove_timer(VncDisplay *vd);
3733e230dd2SCorentin Chary 
3743e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs);
3753e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental,
3763e230dd2SCorentin Chary                                        int x_position, int y_position,
3773e230dd2SCorentin Chary                                        int w, int h);
3783e230dd2SCorentin Chary static void vnc_refresh(void *opaque);
3793e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd);
3803e230dd2SCorentin Chary 
3813e230dd2SCorentin Chary static inline void vnc_set_bit(uint32_t *d, int k)
3823e230dd2SCorentin Chary {
3833e230dd2SCorentin Chary     d[k >> 5] |= 1 << (k & 0x1f);
3843e230dd2SCorentin Chary }
3853e230dd2SCorentin Chary 
3863e230dd2SCorentin Chary static inline void vnc_clear_bit(uint32_t *d, int k)
3873e230dd2SCorentin Chary {
3883e230dd2SCorentin Chary     d[k >> 5] &= ~(1 << (k & 0x1f));
3893e230dd2SCorentin Chary }
3903e230dd2SCorentin Chary 
3913e230dd2SCorentin Chary static inline void vnc_set_bits(uint32_t *d, int n, int nb_words)
3923e230dd2SCorentin Chary {
3933e230dd2SCorentin Chary     int j;
3943e230dd2SCorentin Chary 
3953e230dd2SCorentin Chary     j = 0;
3963e230dd2SCorentin Chary     while (n >= 32) {
3973e230dd2SCorentin Chary         d[j++] = -1;
3983e230dd2SCorentin Chary         n -= 32;
3993e230dd2SCorentin Chary     }
4003e230dd2SCorentin Chary     if (n > 0)
4013e230dd2SCorentin Chary         d[j++] = (1 << n) - 1;
4023e230dd2SCorentin Chary     while (j < nb_words)
4033e230dd2SCorentin Chary         d[j++] = 0;
4043e230dd2SCorentin Chary }
4053e230dd2SCorentin Chary 
4063e230dd2SCorentin Chary static inline int vnc_get_bit(const uint32_t *d, int k)
4073e230dd2SCorentin Chary {
4083e230dd2SCorentin Chary     return (d[k >> 5] >> (k & 0x1f)) & 1;
4093e230dd2SCorentin Chary }
4103e230dd2SCorentin Chary 
4113e230dd2SCorentin Chary static inline int vnc_and_bits(const uint32_t *d1, const uint32_t *d2,
4123e230dd2SCorentin Chary                                int nb_words)
4133e230dd2SCorentin Chary {
4143e230dd2SCorentin Chary     int i;
4153e230dd2SCorentin Chary     for(i = 0; i < nb_words; i++) {
4163e230dd2SCorentin Chary         if ((d1[i] & d2[i]) != 0)
4173e230dd2SCorentin Chary             return 1;
4183e230dd2SCorentin Chary     }
4193e230dd2SCorentin Chary     return 0;
4203e230dd2SCorentin Chary }
4213e230dd2SCorentin Chary 
4223e230dd2SCorentin Chary static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h)
4233e230dd2SCorentin Chary {
4243e230dd2SCorentin Chary     int i;
4253e230dd2SCorentin Chary     VncDisplay *vd = ds->opaque;
4263e230dd2SCorentin Chary     struct VncSurface *s = &vd->guest;
4273e230dd2SCorentin Chary 
4283e230dd2SCorentin Chary     h += y;
4293e230dd2SCorentin Chary 
4303e230dd2SCorentin Chary     /* round x down to ensure the loop only spans one 16-pixel block per,
4313e230dd2SCorentin Chary        iteration.  otherwise, if (x % 16) != 0, the last iteration may span
4323e230dd2SCorentin Chary        two 16-pixel blocks but we only mark the first as dirty
4333e230dd2SCorentin Chary     */
4343e230dd2SCorentin Chary     w += (x % 16);
4353e230dd2SCorentin Chary     x -= (x % 16);
4363e230dd2SCorentin Chary 
4373e230dd2SCorentin Chary     x = MIN(x, s->ds->width);
4383e230dd2SCorentin Chary     y = MIN(y, s->ds->height);
4393e230dd2SCorentin Chary     w = MIN(x + w, s->ds->width) - x;
4403e230dd2SCorentin Chary     h = MIN(h, s->ds->height);
4413e230dd2SCorentin Chary 
4423e230dd2SCorentin Chary     for (; y < h; y++)
4433e230dd2SCorentin Chary         for (i = 0; i < w; i += 16)
4443e230dd2SCorentin Chary             vnc_set_bit(s->dirty[y], (x + i) / 16);
4453e230dd2SCorentin Chary }
4463e230dd2SCorentin Chary 
4473e230dd2SCorentin Chary void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
4483e230dd2SCorentin Chary                             int32_t encoding)
4493e230dd2SCorentin Chary {
4503e230dd2SCorentin Chary     vnc_write_u16(vs, x);
4513e230dd2SCorentin Chary     vnc_write_u16(vs, y);
4523e230dd2SCorentin Chary     vnc_write_u16(vs, w);
4533e230dd2SCorentin Chary     vnc_write_u16(vs, h);
4543e230dd2SCorentin Chary 
4553e230dd2SCorentin Chary     vnc_write_s32(vs, encoding);
4563e230dd2SCorentin Chary }
4573e230dd2SCorentin Chary 
4583e230dd2SCorentin Chary void buffer_reserve(Buffer *buffer, size_t len)
4593e230dd2SCorentin Chary {
4603e230dd2SCorentin Chary     if ((buffer->capacity - buffer->offset) < len) {
4613e230dd2SCorentin Chary         buffer->capacity += (len + 1024);
4623e230dd2SCorentin Chary         buffer->buffer = qemu_realloc(buffer->buffer, buffer->capacity);
4633e230dd2SCorentin Chary         if (buffer->buffer == NULL) {
4643e230dd2SCorentin Chary             fprintf(stderr, "vnc: out of memory\n");
4653e230dd2SCorentin Chary             exit(1);
4663e230dd2SCorentin Chary         }
4673e230dd2SCorentin Chary     }
4683e230dd2SCorentin Chary }
4693e230dd2SCorentin Chary 
4703e230dd2SCorentin Chary int buffer_empty(Buffer *buffer)
4713e230dd2SCorentin Chary {
4723e230dd2SCorentin Chary     return buffer->offset == 0;
4733e230dd2SCorentin Chary }
4743e230dd2SCorentin Chary 
4753e230dd2SCorentin Chary uint8_t *buffer_end(Buffer *buffer)
4763e230dd2SCorentin Chary {
4773e230dd2SCorentin Chary     return buffer->buffer + buffer->offset;
4783e230dd2SCorentin Chary }
4793e230dd2SCorentin Chary 
4803e230dd2SCorentin Chary void buffer_reset(Buffer *buffer)
4813e230dd2SCorentin Chary {
4823e230dd2SCorentin Chary         buffer->offset = 0;
4833e230dd2SCorentin Chary }
4843e230dd2SCorentin Chary 
4853e230dd2SCorentin Chary void buffer_free(Buffer *buffer)
4863e230dd2SCorentin Chary {
4873e230dd2SCorentin Chary     qemu_free(buffer->buffer);
4883e230dd2SCorentin Chary     buffer->offset = 0;
4893e230dd2SCorentin Chary     buffer->capacity = 0;
4903e230dd2SCorentin Chary     buffer->buffer = NULL;
4913e230dd2SCorentin Chary }
4923e230dd2SCorentin Chary 
4933e230dd2SCorentin Chary void buffer_append(Buffer *buffer, const void *data, size_t len)
4943e230dd2SCorentin Chary {
4953e230dd2SCorentin Chary     memcpy(buffer->buffer + buffer->offset, data, len);
4963e230dd2SCorentin Chary     buffer->offset += len;
4973e230dd2SCorentin Chary }
4983e230dd2SCorentin Chary 
4993e230dd2SCorentin Chary static void vnc_desktop_resize(VncState *vs)
5003e230dd2SCorentin Chary {
5013e230dd2SCorentin Chary     DisplayState *ds = vs->ds;
5023e230dd2SCorentin Chary 
5033e230dd2SCorentin Chary     if (vs->csock == -1 || !vnc_has_feature(vs, VNC_FEATURE_RESIZE)) {
5043e230dd2SCorentin Chary         return;
5053e230dd2SCorentin Chary     }
5063e230dd2SCorentin Chary     if (vs->client_width == ds_get_width(ds) &&
5073e230dd2SCorentin Chary         vs->client_height == ds_get_height(ds)) {
5083e230dd2SCorentin Chary         return;
5093e230dd2SCorentin Chary     }
5103e230dd2SCorentin Chary     vs->client_width = ds_get_width(ds);
5113e230dd2SCorentin Chary     vs->client_height = ds_get_height(ds);
512bd023f95SCorentin Chary     vnc_lock_output(vs);
5133e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
5143e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
5153e230dd2SCorentin Chary     vnc_write_u16(vs, 1); /* number of rects */
5163e230dd2SCorentin Chary     vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height,
5173e230dd2SCorentin Chary                            VNC_ENCODING_DESKTOPRESIZE);
518bd023f95SCorentin Chary     vnc_unlock_output(vs);
5193e230dd2SCorentin Chary     vnc_flush(vs);
5203e230dd2SCorentin Chary }
5213e230dd2SCorentin Chary 
522bd023f95SCorentin Chary #ifdef CONFIG_VNC_THREAD
523bd023f95SCorentin Chary static void vnc_abort_display_jobs(VncDisplay *vd)
524bd023f95SCorentin Chary {
525bd023f95SCorentin Chary     VncState *vs;
526bd023f95SCorentin Chary 
527bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
528bd023f95SCorentin Chary         vnc_lock_output(vs);
529bd023f95SCorentin Chary         vs->abort = true;
530bd023f95SCorentin Chary         vnc_unlock_output(vs);
531bd023f95SCorentin Chary     }
532bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
533bd023f95SCorentin Chary         vnc_jobs_join(vs);
534bd023f95SCorentin Chary     }
535bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
536bd023f95SCorentin Chary         vnc_lock_output(vs);
537bd023f95SCorentin Chary         vs->abort = false;
538bd023f95SCorentin Chary         vnc_unlock_output(vs);
539bd023f95SCorentin Chary     }
540bd023f95SCorentin Chary }
541bd023f95SCorentin Chary #else
542bd023f95SCorentin Chary static void vnc_abort_display_jobs(VncDisplay *vd)
543bd023f95SCorentin Chary {
544bd023f95SCorentin Chary }
545bd023f95SCorentin Chary #endif
546bd023f95SCorentin Chary 
5473e230dd2SCorentin Chary static void vnc_dpy_resize(DisplayState *ds)
5483e230dd2SCorentin Chary {
5493e230dd2SCorentin Chary     VncDisplay *vd = ds->opaque;
5503e230dd2SCorentin Chary     VncState *vs;
5513e230dd2SCorentin Chary 
552bd023f95SCorentin Chary     vnc_abort_display_jobs(vd);
553bd023f95SCorentin Chary 
5543e230dd2SCorentin Chary     /* server surface */
5553e230dd2SCorentin Chary     if (!vd->server)
5563e230dd2SCorentin Chary         vd->server = qemu_mallocz(sizeof(*vd->server));
5573e230dd2SCorentin Chary     if (vd->server->data)
5583e230dd2SCorentin Chary         qemu_free(vd->server->data);
5593e230dd2SCorentin Chary     *(vd->server) = *(ds->surface);
5603e230dd2SCorentin Chary     vd->server->data = qemu_mallocz(vd->server->linesize *
5613e230dd2SCorentin Chary                                     vd->server->height);
5623e230dd2SCorentin Chary 
5633e230dd2SCorentin Chary     /* guest surface */
5643e230dd2SCorentin Chary     if (!vd->guest.ds)
5653e230dd2SCorentin Chary         vd->guest.ds = qemu_mallocz(sizeof(*vd->guest.ds));
5663e230dd2SCorentin Chary     if (ds_get_bytes_per_pixel(ds) != vd->guest.ds->pf.bytes_per_pixel)
5673e230dd2SCorentin Chary         console_color_init(ds);
5683e230dd2SCorentin Chary     *(vd->guest.ds) = *(ds->surface);
5693e230dd2SCorentin Chary     memset(vd->guest.dirty, 0xFF, sizeof(vd->guest.dirty));
5703e230dd2SCorentin Chary 
5713e230dd2SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
5723e230dd2SCorentin Chary         vnc_colordepth(vs);
5733e230dd2SCorentin Chary         vnc_desktop_resize(vs);
5743e230dd2SCorentin Chary         if (vs->vd->cursor) {
5753e230dd2SCorentin Chary             vnc_cursor_define(vs);
5763e230dd2SCorentin Chary         }
5773e230dd2SCorentin Chary         memset(vs->dirty, 0xFF, sizeof(vs->dirty));
5783e230dd2SCorentin Chary     }
5793e230dd2SCorentin Chary }
5803e230dd2SCorentin Chary 
5813e230dd2SCorentin Chary /* fastest code */
5823e230dd2SCorentin Chary static void vnc_write_pixels_copy(VncState *vs, struct PixelFormat *pf,
5833e230dd2SCorentin Chary                                   void *pixels, int size)
5843e230dd2SCorentin Chary {
5853e230dd2SCorentin Chary     vnc_write(vs, pixels, size);
5863e230dd2SCorentin Chary }
5873e230dd2SCorentin Chary 
5883e230dd2SCorentin Chary /* slowest but generic code. */
5893e230dd2SCorentin Chary void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v)
5903e230dd2SCorentin Chary {
5913e230dd2SCorentin Chary     uint8_t r, g, b;
5923e230dd2SCorentin Chary     VncDisplay *vd = vs->vd;
5933e230dd2SCorentin Chary 
5943e230dd2SCorentin Chary     r = ((((v & vd->server->pf.rmask) >> vd->server->pf.rshift) << vs->clientds.pf.rbits) >>
5953e230dd2SCorentin Chary         vd->server->pf.rbits);
5963e230dd2SCorentin Chary     g = ((((v & vd->server->pf.gmask) >> vd->server->pf.gshift) << vs->clientds.pf.gbits) >>
5973e230dd2SCorentin Chary         vd->server->pf.gbits);
5983e230dd2SCorentin Chary     b = ((((v & vd->server->pf.bmask) >> vd->server->pf.bshift) << vs->clientds.pf.bbits) >>
5993e230dd2SCorentin Chary         vd->server->pf.bbits);
6003e230dd2SCorentin Chary     v = (r << vs->clientds.pf.rshift) |
6013e230dd2SCorentin Chary         (g << vs->clientds.pf.gshift) |
6023e230dd2SCorentin Chary         (b << vs->clientds.pf.bshift);
6033e230dd2SCorentin Chary     switch(vs->clientds.pf.bytes_per_pixel) {
6043e230dd2SCorentin Chary     case 1:
6053e230dd2SCorentin Chary         buf[0] = v;
6063e230dd2SCorentin Chary         break;
6073e230dd2SCorentin Chary     case 2:
6083e230dd2SCorentin Chary         if (vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) {
6093e230dd2SCorentin Chary             buf[0] = v >> 8;
6103e230dd2SCorentin Chary             buf[1] = v;
6113e230dd2SCorentin Chary         } else {
6123e230dd2SCorentin Chary             buf[1] = v >> 8;
6133e230dd2SCorentin Chary             buf[0] = v;
6143e230dd2SCorentin Chary         }
6153e230dd2SCorentin Chary         break;
6163e230dd2SCorentin Chary     default:
6173e230dd2SCorentin Chary     case 4:
6183e230dd2SCorentin Chary         if (vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) {
6193e230dd2SCorentin Chary             buf[0] = v >> 24;
6203e230dd2SCorentin Chary             buf[1] = v >> 16;
6213e230dd2SCorentin Chary             buf[2] = v >> 8;
6223e230dd2SCorentin Chary             buf[3] = v;
6233e230dd2SCorentin Chary         } else {
6243e230dd2SCorentin Chary             buf[3] = v >> 24;
6253e230dd2SCorentin Chary             buf[2] = v >> 16;
6263e230dd2SCorentin Chary             buf[1] = v >> 8;
6273e230dd2SCorentin Chary             buf[0] = v;
6283e230dd2SCorentin Chary         }
6293e230dd2SCorentin Chary         break;
6303e230dd2SCorentin Chary     }
6313e230dd2SCorentin Chary }
6323e230dd2SCorentin Chary 
6333e230dd2SCorentin Chary static void vnc_write_pixels_generic(VncState *vs, struct PixelFormat *pf,
6343e230dd2SCorentin Chary                                      void *pixels1, int size)
6353e230dd2SCorentin Chary {
6363e230dd2SCorentin Chary     uint8_t buf[4];
6373e230dd2SCorentin Chary 
6383e230dd2SCorentin Chary     if (pf->bytes_per_pixel == 4) {
6393e230dd2SCorentin Chary         uint32_t *pixels = pixels1;
6403e230dd2SCorentin Chary         int n, i;
6413e230dd2SCorentin Chary         n = size >> 2;
6423e230dd2SCorentin Chary         for(i = 0; i < n; i++) {
6433e230dd2SCorentin Chary             vnc_convert_pixel(vs, buf, pixels[i]);
6443e230dd2SCorentin Chary             vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel);
6453e230dd2SCorentin Chary         }
6463e230dd2SCorentin Chary     } else if (pf->bytes_per_pixel == 2) {
6473e230dd2SCorentin Chary         uint16_t *pixels = pixels1;
6483e230dd2SCorentin Chary         int n, i;
6493e230dd2SCorentin Chary         n = size >> 1;
6503e230dd2SCorentin Chary         for(i = 0; i < n; i++) {
6513e230dd2SCorentin Chary             vnc_convert_pixel(vs, buf, pixels[i]);
6523e230dd2SCorentin Chary             vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel);
6533e230dd2SCorentin Chary         }
6543e230dd2SCorentin Chary     } else if (pf->bytes_per_pixel == 1) {
6553e230dd2SCorentin Chary         uint8_t *pixels = pixels1;
6563e230dd2SCorentin Chary         int n, i;
6573e230dd2SCorentin Chary         n = size;
6583e230dd2SCorentin Chary         for(i = 0; i < n; i++) {
6593e230dd2SCorentin Chary             vnc_convert_pixel(vs, buf, pixels[i]);
6603e230dd2SCorentin Chary             vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel);
6613e230dd2SCorentin Chary         }
6623e230dd2SCorentin Chary     } else {
6633e230dd2SCorentin Chary         fprintf(stderr, "vnc_write_pixels_generic: VncState color depth not supported\n");
6643e230dd2SCorentin Chary     }
6653e230dd2SCorentin Chary }
6663e230dd2SCorentin Chary 
6673e230dd2SCorentin Chary int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
6683e230dd2SCorentin Chary {
6693e230dd2SCorentin Chary     int i;
6703e230dd2SCorentin Chary     uint8_t *row;
6713e230dd2SCorentin Chary     VncDisplay *vd = vs->vd;
6723e230dd2SCorentin Chary 
6733e230dd2SCorentin Chary     row = vd->server->data + y * ds_get_linesize(vs->ds) + x * ds_get_bytes_per_pixel(vs->ds);
6743e230dd2SCorentin Chary     for (i = 0; i < h; i++) {
6753e230dd2SCorentin Chary         vs->write_pixels(vs, &vd->server->pf, row, w * ds_get_bytes_per_pixel(vs->ds));
6763e230dd2SCorentin Chary         row += ds_get_linesize(vs->ds);
6773e230dd2SCorentin Chary     }
6783e230dd2SCorentin Chary     return 1;
6793e230dd2SCorentin Chary }
6803e230dd2SCorentin Chary 
681bd023f95SCorentin Chary int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
6823e230dd2SCorentin Chary {
6833e230dd2SCorentin Chary     int n = 0;
6843e230dd2SCorentin Chary 
6853e230dd2SCorentin Chary     switch(vs->vnc_encoding) {
6863e230dd2SCorentin Chary         case VNC_ENCODING_ZLIB:
6873e230dd2SCorentin Chary             n = vnc_zlib_send_framebuffer_update(vs, x, y, w, h);
6883e230dd2SCorentin Chary             break;
6893e230dd2SCorentin Chary         case VNC_ENCODING_HEXTILE:
6903e230dd2SCorentin Chary             vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE);
6913e230dd2SCorentin Chary             n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h);
6923e230dd2SCorentin Chary             break;
6933e230dd2SCorentin Chary         case VNC_ENCODING_TIGHT:
6943e230dd2SCorentin Chary             n = vnc_tight_send_framebuffer_update(vs, x, y, w, h);
6953e230dd2SCorentin Chary             break;
696efe556adSCorentin Chary         case VNC_ENCODING_TIGHT_PNG:
697efe556adSCorentin Chary             n = vnc_tight_png_send_framebuffer_update(vs, x, y, w, h);
698efe556adSCorentin Chary             break;
6993e230dd2SCorentin Chary         default:
7003e230dd2SCorentin Chary             vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW);
7013e230dd2SCorentin Chary             n = vnc_raw_send_framebuffer_update(vs, x, y, w, h);
7023e230dd2SCorentin Chary             break;
7033e230dd2SCorentin Chary     }
7043e230dd2SCorentin Chary     return n;
7053e230dd2SCorentin Chary }
7063e230dd2SCorentin Chary 
7073e230dd2SCorentin Chary static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
7083e230dd2SCorentin Chary {
7093e230dd2SCorentin Chary     /* send bitblit op to the vnc client */
710bd023f95SCorentin Chary     vnc_lock_output(vs);
7113e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
7123e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
7133e230dd2SCorentin Chary     vnc_write_u16(vs, 1); /* number of rects */
7143e230dd2SCorentin Chary     vnc_framebuffer_update(vs, dst_x, dst_y, w, h, VNC_ENCODING_COPYRECT);
7153e230dd2SCorentin Chary     vnc_write_u16(vs, src_x);
7163e230dd2SCorentin Chary     vnc_write_u16(vs, src_y);
717bd023f95SCorentin Chary     vnc_unlock_output(vs);
7183e230dd2SCorentin Chary     vnc_flush(vs);
7193e230dd2SCorentin Chary }
7203e230dd2SCorentin Chary 
7213e230dd2SCorentin Chary static void vnc_dpy_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
7223e230dd2SCorentin Chary {
7233e230dd2SCorentin Chary     VncDisplay *vd = ds->opaque;
7243e230dd2SCorentin Chary     VncState *vs, *vn;
7253e230dd2SCorentin Chary     uint8_t *src_row;
7263e230dd2SCorentin Chary     uint8_t *dst_row;
7273e230dd2SCorentin Chary     int i,x,y,pitch,depth,inc,w_lim,s;
7283e230dd2SCorentin Chary     int cmp_bytes;
7293e230dd2SCorentin Chary 
7303e230dd2SCorentin Chary     vnc_refresh_server_surface(vd);
7313e230dd2SCorentin Chary     QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
7323e230dd2SCorentin Chary         if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
7333e230dd2SCorentin Chary             vs->force_update = 1;
734bd023f95SCorentin Chary             vnc_update_client_sync(vs, 1);
7353e230dd2SCorentin Chary             /* vs might be free()ed here */
7363e230dd2SCorentin Chary         }
7373e230dd2SCorentin Chary     }
7383e230dd2SCorentin Chary 
7393e230dd2SCorentin Chary     /* do bitblit op on the local surface too */
7403e230dd2SCorentin Chary     pitch = ds_get_linesize(vd->ds);
7413e230dd2SCorentin Chary     depth = ds_get_bytes_per_pixel(vd->ds);
7423e230dd2SCorentin Chary     src_row = vd->server->data + pitch * src_y + depth * src_x;
7433e230dd2SCorentin Chary     dst_row = vd->server->data + pitch * dst_y + depth * dst_x;
7443e230dd2SCorentin Chary     y = dst_y;
7453e230dd2SCorentin Chary     inc = 1;
7463e230dd2SCorentin Chary     if (dst_y > src_y) {
7473e230dd2SCorentin Chary         /* copy backwards */
7483e230dd2SCorentin Chary         src_row += pitch * (h-1);
7493e230dd2SCorentin Chary         dst_row += pitch * (h-1);
7503e230dd2SCorentin Chary         pitch = -pitch;
7513e230dd2SCorentin Chary         y = dst_y + h - 1;
7523e230dd2SCorentin Chary         inc = -1;
7533e230dd2SCorentin Chary     }
7543e230dd2SCorentin Chary     w_lim = w - (16 - (dst_x % 16));
7553e230dd2SCorentin Chary     if (w_lim < 0)
7563e230dd2SCorentin Chary         w_lim = w;
7573e230dd2SCorentin Chary     else
7583e230dd2SCorentin Chary         w_lim = w - (w_lim % 16);
7593e230dd2SCorentin Chary     for (i = 0; i < h; i++) {
7603e230dd2SCorentin Chary         for (x = 0; x <= w_lim;
7613e230dd2SCorentin Chary                 x += s, src_row += cmp_bytes, dst_row += cmp_bytes) {
7623e230dd2SCorentin Chary             if (x == w_lim) {
7633e230dd2SCorentin Chary                 if ((s = w - w_lim) == 0)
7643e230dd2SCorentin Chary                     break;
7653e230dd2SCorentin Chary             } else if (!x) {
7663e230dd2SCorentin Chary                 s = (16 - (dst_x % 16));
7673e230dd2SCorentin Chary                 s = MIN(s, w_lim);
7683e230dd2SCorentin Chary             } else {
7693e230dd2SCorentin Chary                 s = 16;
7703e230dd2SCorentin Chary             }
7713e230dd2SCorentin Chary             cmp_bytes = s * depth;
7723e230dd2SCorentin Chary             if (memcmp(src_row, dst_row, cmp_bytes) == 0)
7733e230dd2SCorentin Chary                 continue;
7743e230dd2SCorentin Chary             memmove(dst_row, src_row, cmp_bytes);
7753e230dd2SCorentin Chary             QTAILQ_FOREACH(vs, &vd->clients, next) {
7763e230dd2SCorentin Chary                 if (!vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
7773e230dd2SCorentin Chary                     vnc_set_bit(vs->dirty[y], ((x + dst_x) / 16));
7783e230dd2SCorentin Chary                 }
7793e230dd2SCorentin Chary             }
7803e230dd2SCorentin Chary         }
7813e230dd2SCorentin Chary         src_row += pitch - w * depth;
7823e230dd2SCorentin Chary         dst_row += pitch - w * depth;
7833e230dd2SCorentin Chary         y += inc;
7843e230dd2SCorentin Chary     }
7853e230dd2SCorentin Chary 
7863e230dd2SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
7873e230dd2SCorentin Chary         if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
7883e230dd2SCorentin Chary             vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h);
7893e230dd2SCorentin Chary         }
7903e230dd2SCorentin Chary     }
7913e230dd2SCorentin Chary }
7923e230dd2SCorentin Chary 
7933e230dd2SCorentin Chary static void vnc_mouse_set(int x, int y, int visible)
7943e230dd2SCorentin Chary {
7953e230dd2SCorentin Chary     /* can we ask the client(s) to move the pointer ??? */
7963e230dd2SCorentin Chary }
7973e230dd2SCorentin Chary 
7983e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs)
7993e230dd2SCorentin Chary {
8003e230dd2SCorentin Chary     QEMUCursor *c = vs->vd->cursor;
8013e230dd2SCorentin Chary     PixelFormat pf = qemu_default_pixelformat(32);
8023e230dd2SCorentin Chary     int isize;
8033e230dd2SCorentin Chary 
8043e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) {
805d01f9595SCorentin Chary         vnc_lock_output(vs);
8063e230dd2SCorentin Chary         vnc_write_u8(vs,  VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
8073e230dd2SCorentin Chary         vnc_write_u8(vs,  0);  /*  padding     */
8083e230dd2SCorentin Chary         vnc_write_u16(vs, 1);  /*  # of rects  */
8093e230dd2SCorentin Chary         vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height,
8103e230dd2SCorentin Chary                                VNC_ENCODING_RICH_CURSOR);
8113e230dd2SCorentin Chary         isize = c->width * c->height * vs->clientds.pf.bytes_per_pixel;
8123e230dd2SCorentin Chary         vnc_write_pixels_generic(vs, &pf, c->data, isize);
8133e230dd2SCorentin Chary         vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize);
814d01f9595SCorentin Chary         vnc_unlock_output(vs);
8153e230dd2SCorentin Chary         return 0;
8163e230dd2SCorentin Chary     }
8173e230dd2SCorentin Chary     return -1;
8183e230dd2SCorentin Chary }
8193e230dd2SCorentin Chary 
8203e230dd2SCorentin Chary static void vnc_dpy_cursor_define(QEMUCursor *c)
8213e230dd2SCorentin Chary {
8223e230dd2SCorentin Chary     VncDisplay *vd = vnc_display;
8233e230dd2SCorentin Chary     VncState *vs;
8243e230dd2SCorentin Chary 
8253e230dd2SCorentin Chary     cursor_put(vd->cursor);
8263e230dd2SCorentin Chary     qemu_free(vd->cursor_mask);
8273e230dd2SCorentin Chary 
8283e230dd2SCorentin Chary     vd->cursor = c;
8293e230dd2SCorentin Chary     cursor_get(vd->cursor);
8303e230dd2SCorentin Chary     vd->cursor_msize = cursor_get_mono_bpl(c) * c->height;
8313e230dd2SCorentin Chary     vd->cursor_mask = qemu_mallocz(vd->cursor_msize);
8323e230dd2SCorentin Chary     cursor_get_mono_mask(c, 0, vd->cursor_mask);
8333e230dd2SCorentin Chary 
8343e230dd2SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
8353e230dd2SCorentin Chary         vnc_cursor_define(vs);
8363e230dd2SCorentin Chary     }
8373e230dd2SCorentin Chary }
8383e230dd2SCorentin Chary 
8393e230dd2SCorentin Chary static int find_and_clear_dirty_height(struct VncState *vs,
8403e230dd2SCorentin Chary                                        int y, int last_x, int x)
8413e230dd2SCorentin Chary {
8423e230dd2SCorentin Chary     int h;
8433e230dd2SCorentin Chary     VncDisplay *vd = vs->vd;
8443e230dd2SCorentin Chary 
8453e230dd2SCorentin Chary     for (h = 1; h < (vd->server->height - y); h++) {
8463e230dd2SCorentin Chary         int tmp_x;
8473e230dd2SCorentin Chary         if (!vnc_get_bit(vs->dirty[y + h], last_x))
8483e230dd2SCorentin Chary             break;
8493e230dd2SCorentin Chary         for (tmp_x = last_x; tmp_x < x; tmp_x++)
8503e230dd2SCorentin Chary             vnc_clear_bit(vs->dirty[y + h], tmp_x);
8513e230dd2SCorentin Chary     }
8523e230dd2SCorentin Chary 
8533e230dd2SCorentin Chary     return h;
8543e230dd2SCorentin Chary }
8553e230dd2SCorentin Chary 
856bd023f95SCorentin Chary #ifdef CONFIG_VNC_THREAD
857bd023f95SCorentin Chary static int vnc_update_client_sync(VncState *vs, int has_dirty)
858bd023f95SCorentin Chary {
859bd023f95SCorentin Chary     int ret = vnc_update_client(vs, has_dirty);
860bd023f95SCorentin Chary     vnc_jobs_join(vs);
861bd023f95SCorentin Chary     return ret;
862bd023f95SCorentin Chary }
863bd023f95SCorentin Chary #else
864bd023f95SCorentin Chary static int vnc_update_client_sync(VncState *vs, int has_dirty)
865bd023f95SCorentin Chary {
866bd023f95SCorentin Chary     return vnc_update_client(vs, has_dirty);
867bd023f95SCorentin Chary }
868bd023f95SCorentin Chary #endif
869bd023f95SCorentin Chary 
8703e230dd2SCorentin Chary static int vnc_update_client(VncState *vs, int has_dirty)
8713e230dd2SCorentin Chary {
8723e230dd2SCorentin Chary     if (vs->need_update && vs->csock != -1) {
8733e230dd2SCorentin Chary         VncDisplay *vd = vs->vd;
874bd023f95SCorentin Chary         VncJob *job;
8753e230dd2SCorentin Chary         int y;
8763e230dd2SCorentin Chary         int width, height;
877bd023f95SCorentin Chary         int n = 0;
878bd023f95SCorentin Chary 
8793e230dd2SCorentin Chary 
8803e230dd2SCorentin Chary         if (vs->output.offset && !vs->audio_cap && !vs->force_update)
8813e230dd2SCorentin Chary             /* kernel send buffers are full -> drop frames to throttle */
8823e230dd2SCorentin Chary             return 0;
8833e230dd2SCorentin Chary 
8843e230dd2SCorentin Chary         if (!has_dirty && !vs->audio_cap && !vs->force_update)
8853e230dd2SCorentin Chary             return 0;
8863e230dd2SCorentin Chary 
8873e230dd2SCorentin Chary         /*
8883e230dd2SCorentin Chary          * Send screen updates to the vnc client using the server
8893e230dd2SCorentin Chary          * surface and server dirty map.  guest surface updates
8903e230dd2SCorentin Chary          * happening in parallel don't disturb us, the next pass will
8913e230dd2SCorentin Chary          * send them to the client.
8923e230dd2SCorentin Chary          */
893bd023f95SCorentin Chary         job = vnc_job_new(vs);
8943e230dd2SCorentin Chary 
8953e230dd2SCorentin Chary         width = MIN(vd->server->width, vs->client_width);
8963e230dd2SCorentin Chary         height = MIN(vd->server->height, vs->client_height);
8973e230dd2SCorentin Chary 
8983e230dd2SCorentin Chary         for (y = 0; y < height; y++) {
8993e230dd2SCorentin Chary             int x;
9003e230dd2SCorentin Chary             int last_x = -1;
9013e230dd2SCorentin Chary             for (x = 0; x < width / 16; x++) {
9023e230dd2SCorentin Chary                 if (vnc_get_bit(vs->dirty[y], x)) {
9033e230dd2SCorentin Chary                     if (last_x == -1) {
9043e230dd2SCorentin Chary                         last_x = x;
9053e230dd2SCorentin Chary                     }
9063e230dd2SCorentin Chary                     vnc_clear_bit(vs->dirty[y], x);
9073e230dd2SCorentin Chary                 } else {
9083e230dd2SCorentin Chary                     if (last_x != -1) {
9093e230dd2SCorentin Chary                         int h = find_and_clear_dirty_height(vs, y, last_x, x);
910bd023f95SCorentin Chary 
911bd023f95SCorentin Chary                         n += vnc_job_add_rect(job, last_x * 16, y,
9123e230dd2SCorentin Chary                                               (x - last_x) * 16, h);
9133e230dd2SCorentin Chary                     }
9143e230dd2SCorentin Chary                     last_x = -1;
9153e230dd2SCorentin Chary                 }
9163e230dd2SCorentin Chary             }
9173e230dd2SCorentin Chary             if (last_x != -1) {
9183e230dd2SCorentin Chary                 int h = find_and_clear_dirty_height(vs, y, last_x, x);
919bd023f95SCorentin Chary                 n += vnc_job_add_rect(job, last_x * 16, y,
9203e230dd2SCorentin Chary                                       (x - last_x) * 16, h);
9213e230dd2SCorentin Chary             }
9223e230dd2SCorentin Chary         }
923bd023f95SCorentin Chary 
924bd023f95SCorentin Chary         vnc_job_push(job);
9253e230dd2SCorentin Chary         vs->force_update = 0;
926bd023f95SCorentin Chary         return n;
9273e230dd2SCorentin Chary     }
9283e230dd2SCorentin Chary 
9293e230dd2SCorentin Chary     if (vs->csock == -1)
9303e230dd2SCorentin Chary         vnc_disconnect_finish(vs);
9313e230dd2SCorentin Chary 
9323e230dd2SCorentin Chary     return 0;
9333e230dd2SCorentin Chary }
9343e230dd2SCorentin Chary 
9353e230dd2SCorentin Chary /* audio */
9363e230dd2SCorentin Chary static void audio_capture_notify(void *opaque, audcnotification_e cmd)
9373e230dd2SCorentin Chary {
9383e230dd2SCorentin Chary     VncState *vs = opaque;
9393e230dd2SCorentin Chary 
9403e230dd2SCorentin Chary     switch (cmd) {
9413e230dd2SCorentin Chary     case AUD_CNOTIFY_DISABLE:
942bd023f95SCorentin Chary         vnc_lock_output(vs);
9433e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
9443e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
9453e230dd2SCorentin Chary         vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END);
946bd023f95SCorentin Chary         vnc_unlock_output(vs);
9473e230dd2SCorentin Chary         vnc_flush(vs);
9483e230dd2SCorentin Chary         break;
9493e230dd2SCorentin Chary 
9503e230dd2SCorentin Chary     case AUD_CNOTIFY_ENABLE:
951bd023f95SCorentin Chary         vnc_lock_output(vs);
9523e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
9533e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
9543e230dd2SCorentin Chary         vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN);
955bd023f95SCorentin Chary         vnc_unlock_output(vs);
9563e230dd2SCorentin Chary         vnc_flush(vs);
9573e230dd2SCorentin Chary         break;
9583e230dd2SCorentin Chary     }
9593e230dd2SCorentin Chary }
9603e230dd2SCorentin Chary 
9613e230dd2SCorentin Chary static void audio_capture_destroy(void *opaque)
9623e230dd2SCorentin Chary {
9633e230dd2SCorentin Chary }
9643e230dd2SCorentin Chary 
9653e230dd2SCorentin Chary static void audio_capture(void *opaque, void *buf, int size)
9663e230dd2SCorentin Chary {
9673e230dd2SCorentin Chary     VncState *vs = opaque;
9683e230dd2SCorentin Chary 
969bd023f95SCorentin Chary     vnc_lock_output(vs);
9703e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
9713e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
9723e230dd2SCorentin Chary     vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA);
9733e230dd2SCorentin Chary     vnc_write_u32(vs, size);
9743e230dd2SCorentin Chary     vnc_write(vs, buf, size);
975bd023f95SCorentin Chary     vnc_unlock_output(vs);
9763e230dd2SCorentin Chary     vnc_flush(vs);
9773e230dd2SCorentin Chary }
9783e230dd2SCorentin Chary 
9793e230dd2SCorentin Chary static void audio_add(VncState *vs)
9803e230dd2SCorentin Chary {
9813e230dd2SCorentin Chary     struct audio_capture_ops ops;
9823e230dd2SCorentin Chary 
9833e230dd2SCorentin Chary     if (vs->audio_cap) {
9843e230dd2SCorentin Chary         monitor_printf(default_mon, "audio already running\n");
9853e230dd2SCorentin Chary         return;
9863e230dd2SCorentin Chary     }
9873e230dd2SCorentin Chary 
9883e230dd2SCorentin Chary     ops.notify = audio_capture_notify;
9893e230dd2SCorentin Chary     ops.destroy = audio_capture_destroy;
9903e230dd2SCorentin Chary     ops.capture = audio_capture;
9913e230dd2SCorentin Chary 
9923e230dd2SCorentin Chary     vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs);
9933e230dd2SCorentin Chary     if (!vs->audio_cap) {
9943e230dd2SCorentin Chary         monitor_printf(default_mon, "Failed to add audio capture\n");
9953e230dd2SCorentin Chary     }
9963e230dd2SCorentin Chary }
9973e230dd2SCorentin Chary 
9983e230dd2SCorentin Chary static void audio_del(VncState *vs)
9993e230dd2SCorentin Chary {
10003e230dd2SCorentin Chary     if (vs->audio_cap) {
10013e230dd2SCorentin Chary         AUD_del_capture(vs->audio_cap, vs);
10023e230dd2SCorentin Chary         vs->audio_cap = NULL;
10033e230dd2SCorentin Chary     }
10043e230dd2SCorentin Chary }
10053e230dd2SCorentin Chary 
10063e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs)
10073e230dd2SCorentin Chary {
10083e230dd2SCorentin Chary     if (vs->csock == -1)
10093e230dd2SCorentin Chary         return;
10103e230dd2SCorentin Chary     qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
10113e230dd2SCorentin Chary     closesocket(vs->csock);
10123e230dd2SCorentin Chary     vs->csock = -1;
10133e230dd2SCorentin Chary }
10143e230dd2SCorentin Chary 
10153e230dd2SCorentin Chary static void vnc_disconnect_finish(VncState *vs)
10163e230dd2SCorentin Chary {
1017*7d964c9dSCorentin Chary     int i;
1018*7d964c9dSCorentin Chary 
1019bd023f95SCorentin Chary     vnc_jobs_join(vs); /* Wait encoding jobs */
1020bd023f95SCorentin Chary 
1021bd023f95SCorentin Chary     vnc_lock_output(vs);
10223e230dd2SCorentin Chary     vnc_qmp_event(vs, QEVENT_VNC_DISCONNECTED);
10233e230dd2SCorentin Chary 
10243e230dd2SCorentin Chary     buffer_free(&vs->input);
10253e230dd2SCorentin Chary     buffer_free(&vs->output);
10263e230dd2SCorentin Chary 
10273e230dd2SCorentin Chary     qobject_decref(vs->info);
10283e230dd2SCorentin Chary 
10293e230dd2SCorentin Chary     vnc_zlib_clear(vs);
10303e230dd2SCorentin Chary     vnc_tight_clear(vs);
10313e230dd2SCorentin Chary 
10323e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
10333e230dd2SCorentin Chary     vnc_tls_client_cleanup(vs);
10343e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
10353e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
10363e230dd2SCorentin Chary     vnc_sasl_client_cleanup(vs);
10373e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
10383e230dd2SCorentin Chary     audio_del(vs);
10393e230dd2SCorentin Chary 
10403e230dd2SCorentin Chary     QTAILQ_REMOVE(&vs->vd->clients, vs, next);
10413e230dd2SCorentin Chary 
10423e230dd2SCorentin Chary     if (QTAILQ_EMPTY(&vs->vd->clients)) {
10433e230dd2SCorentin Chary         dcl->idle = 1;
10443e230dd2SCorentin Chary     }
10453e230dd2SCorentin Chary 
10463e230dd2SCorentin Chary     qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
10473e230dd2SCorentin Chary     vnc_remove_timer(vs->vd);
10483e230dd2SCorentin Chary     if (vs->vd->lock_key_sync)
10493e230dd2SCorentin Chary         qemu_remove_led_event_handler(vs->led);
1050bd023f95SCorentin Chary     vnc_unlock_output(vs);
1051bd023f95SCorentin Chary 
1052bd023f95SCorentin Chary #ifdef CONFIG_VNC_THREAD
1053bd023f95SCorentin Chary     qemu_mutex_destroy(&vs->output_mutex);
1054bd023f95SCorentin Chary #endif
1055*7d964c9dSCorentin Chary     for (i = 0; i < VNC_STAT_ROWS; ++i) {
1056*7d964c9dSCorentin Chary         qemu_free(vs->lossy_rect[i]);
1057*7d964c9dSCorentin Chary     }
1058*7d964c9dSCorentin Chary     qemu_free(vs->lossy_rect);
10593e230dd2SCorentin Chary     qemu_free(vs);
10603e230dd2SCorentin Chary }
10613e230dd2SCorentin Chary 
10623e230dd2SCorentin Chary int vnc_client_io_error(VncState *vs, int ret, int last_errno)
10633e230dd2SCorentin Chary {
10643e230dd2SCorentin Chary     if (ret == 0 || ret == -1) {
10653e230dd2SCorentin Chary         if (ret == -1) {
10663e230dd2SCorentin Chary             switch (last_errno) {
10673e230dd2SCorentin Chary                 case EINTR:
10683e230dd2SCorentin Chary                 case EAGAIN:
10693e230dd2SCorentin Chary #ifdef _WIN32
10703e230dd2SCorentin Chary                 case WSAEWOULDBLOCK:
10713e230dd2SCorentin Chary #endif
10723e230dd2SCorentin Chary                     return 0;
10733e230dd2SCorentin Chary                 default:
10743e230dd2SCorentin Chary                     break;
10753e230dd2SCorentin Chary             }
10763e230dd2SCorentin Chary         }
10773e230dd2SCorentin Chary 
10783e230dd2SCorentin Chary         VNC_DEBUG("Closing down client sock: ret %d, errno %d\n",
10793e230dd2SCorentin Chary                   ret, ret < 0 ? last_errno : 0);
10803e230dd2SCorentin Chary         vnc_disconnect_start(vs);
10813e230dd2SCorentin Chary 
10823e230dd2SCorentin Chary         return 0;
10833e230dd2SCorentin Chary     }
10843e230dd2SCorentin Chary     return ret;
10853e230dd2SCorentin Chary }
10863e230dd2SCorentin Chary 
10873e230dd2SCorentin Chary 
10883e230dd2SCorentin Chary void vnc_client_error(VncState *vs)
10893e230dd2SCorentin Chary {
10903e230dd2SCorentin Chary     VNC_DEBUG("Closing down client sock: protocol error\n");
10913e230dd2SCorentin Chary     vnc_disconnect_start(vs);
10923e230dd2SCorentin Chary }
10933e230dd2SCorentin Chary 
10943e230dd2SCorentin Chary 
10953e230dd2SCorentin Chary /*
10963e230dd2SCorentin Chary  * Called to write a chunk of data to the client socket. The data may
10973e230dd2SCorentin Chary  * be the raw data, or may have already been encoded by SASL.
10983e230dd2SCorentin Chary  * The data will be written either straight onto the socket, or
10993e230dd2SCorentin Chary  * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
11003e230dd2SCorentin Chary  *
11013e230dd2SCorentin Chary  * NB, it is theoretically possible to have 2 layers of encryption,
11023e230dd2SCorentin Chary  * both SASL, and this TLS layer. It is highly unlikely in practice
11033e230dd2SCorentin Chary  * though, since SASL encryption will typically be a no-op if TLS
11043e230dd2SCorentin Chary  * is active
11053e230dd2SCorentin Chary  *
11063e230dd2SCorentin Chary  * Returns the number of bytes written, which may be less than
11073e230dd2SCorentin Chary  * the requested 'datalen' if the socket would block. Returns
11083e230dd2SCorentin Chary  * -1 on error, and disconnects the client socket.
11093e230dd2SCorentin Chary  */
11103e230dd2SCorentin Chary long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
11113e230dd2SCorentin Chary {
11123e230dd2SCorentin Chary     long ret;
11133e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
11143e230dd2SCorentin Chary     if (vs->tls.session) {
11153e230dd2SCorentin Chary         ret = gnutls_write(vs->tls.session, data, datalen);
11163e230dd2SCorentin Chary         if (ret < 0) {
11173e230dd2SCorentin Chary             if (ret == GNUTLS_E_AGAIN)
11183e230dd2SCorentin Chary                 errno = EAGAIN;
11193e230dd2SCorentin Chary             else
11203e230dd2SCorentin Chary                 errno = EIO;
11213e230dd2SCorentin Chary             ret = -1;
11223e230dd2SCorentin Chary         }
11233e230dd2SCorentin Chary     } else
11243e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
11253e230dd2SCorentin Chary         ret = send(vs->csock, (const void *)data, datalen, 0);
11263e230dd2SCorentin Chary     VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret);
11273e230dd2SCorentin Chary     return vnc_client_io_error(vs, ret, socket_error());
11283e230dd2SCorentin Chary }
11293e230dd2SCorentin Chary 
11303e230dd2SCorentin Chary 
11313e230dd2SCorentin Chary /*
11323e230dd2SCorentin Chary  * Called to write buffered data to the client socket, when not
11333e230dd2SCorentin Chary  * using any SASL SSF encryption layers. Will write as much data
11343e230dd2SCorentin Chary  * as possible without blocking. If all buffered data is written,
11353e230dd2SCorentin Chary  * will switch the FD poll() handler back to read monitoring.
11363e230dd2SCorentin Chary  *
11373e230dd2SCorentin Chary  * Returns the number of bytes written, which may be less than
11383e230dd2SCorentin Chary  * the buffered output data if the socket would block. Returns
11393e230dd2SCorentin Chary  * -1 on error, and disconnects the client socket.
11403e230dd2SCorentin Chary  */
11413e230dd2SCorentin Chary static long vnc_client_write_plain(VncState *vs)
11423e230dd2SCorentin Chary {
11433e230dd2SCorentin Chary     long ret;
11443e230dd2SCorentin Chary 
11453e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
11463e230dd2SCorentin Chary     VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
11473e230dd2SCorentin Chary               vs->output.buffer, vs->output.capacity, vs->output.offset,
11483e230dd2SCorentin Chary               vs->sasl.waitWriteSSF);
11493e230dd2SCorentin Chary 
11503e230dd2SCorentin Chary     if (vs->sasl.conn &&
11513e230dd2SCorentin Chary         vs->sasl.runSSF &&
11523e230dd2SCorentin Chary         vs->sasl.waitWriteSSF) {
11533e230dd2SCorentin Chary         ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF);
11543e230dd2SCorentin Chary         if (ret)
11553e230dd2SCorentin Chary             vs->sasl.waitWriteSSF -= ret;
11563e230dd2SCorentin Chary     } else
11573e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
11583e230dd2SCorentin Chary         ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset);
11593e230dd2SCorentin Chary     if (!ret)
11603e230dd2SCorentin Chary         return 0;
11613e230dd2SCorentin Chary 
11623e230dd2SCorentin Chary     memmove(vs->output.buffer, vs->output.buffer + ret, (vs->output.offset - ret));
11633e230dd2SCorentin Chary     vs->output.offset -= ret;
11643e230dd2SCorentin Chary 
11653e230dd2SCorentin Chary     if (vs->output.offset == 0) {
11663e230dd2SCorentin Chary         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
11673e230dd2SCorentin Chary     }
11683e230dd2SCorentin Chary 
11693e230dd2SCorentin Chary     return ret;
11703e230dd2SCorentin Chary }
11713e230dd2SCorentin Chary 
11723e230dd2SCorentin Chary 
11733e230dd2SCorentin Chary /*
11743e230dd2SCorentin Chary  * First function called whenever there is data to be written to
11753e230dd2SCorentin Chary  * the client socket. Will delegate actual work according to whether
11763e230dd2SCorentin Chary  * SASL SSF layers are enabled (thus requiring encryption calls)
11773e230dd2SCorentin Chary  */
1178bd023f95SCorentin Chary static void vnc_client_write_locked(void *opaque)
11793e230dd2SCorentin Chary {
11803e230dd2SCorentin Chary     VncState *vs = opaque;
11813e230dd2SCorentin Chary 
11823e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
11833e230dd2SCorentin Chary     if (vs->sasl.conn &&
11843e230dd2SCorentin Chary         vs->sasl.runSSF &&
11853e230dd2SCorentin Chary         !vs->sasl.waitWriteSSF) {
11863e230dd2SCorentin Chary         vnc_client_write_sasl(vs);
11873e230dd2SCorentin Chary     } else
11883e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
11893e230dd2SCorentin Chary         vnc_client_write_plain(vs);
11903e230dd2SCorentin Chary }
11913e230dd2SCorentin Chary 
1192bd023f95SCorentin Chary void vnc_client_write(void *opaque)
1193bd023f95SCorentin Chary {
1194bd023f95SCorentin Chary     VncState *vs = opaque;
1195bd023f95SCorentin Chary 
1196bd023f95SCorentin Chary     vnc_lock_output(vs);
1197bd023f95SCorentin Chary     if (vs->output.offset) {
1198bd023f95SCorentin Chary         vnc_client_write_locked(opaque);
1199ac71103dSYoshiaki Tamura     } else if (vs->csock != -1) {
1200bd023f95SCorentin Chary         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
1201bd023f95SCorentin Chary     }
1202bd023f95SCorentin Chary     vnc_unlock_output(vs);
1203bd023f95SCorentin Chary }
1204bd023f95SCorentin Chary 
12053e230dd2SCorentin Chary void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
12063e230dd2SCorentin Chary {
12073e230dd2SCorentin Chary     vs->read_handler = func;
12083e230dd2SCorentin Chary     vs->read_handler_expect = expecting;
12093e230dd2SCorentin Chary }
12103e230dd2SCorentin Chary 
12113e230dd2SCorentin Chary 
12123e230dd2SCorentin Chary /*
12133e230dd2SCorentin Chary  * Called to read a chunk of data from the client socket. The data may
12143e230dd2SCorentin Chary  * be the raw data, or may need to be further decoded by SASL.
12153e230dd2SCorentin Chary  * The data will be read either straight from to the socket, or
12163e230dd2SCorentin Chary  * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
12173e230dd2SCorentin Chary  *
12183e230dd2SCorentin Chary  * NB, it is theoretically possible to have 2 layers of encryption,
12193e230dd2SCorentin Chary  * both SASL, and this TLS layer. It is highly unlikely in practice
12203e230dd2SCorentin Chary  * though, since SASL encryption will typically be a no-op if TLS
12213e230dd2SCorentin Chary  * is active
12223e230dd2SCorentin Chary  *
12233e230dd2SCorentin Chary  * Returns the number of bytes read, which may be less than
12243e230dd2SCorentin Chary  * the requested 'datalen' if the socket would block. Returns
12253e230dd2SCorentin Chary  * -1 on error, and disconnects the client socket.
12263e230dd2SCorentin Chary  */
12273e230dd2SCorentin Chary long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
12283e230dd2SCorentin Chary {
12293e230dd2SCorentin Chary     long ret;
12303e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
12313e230dd2SCorentin Chary     if (vs->tls.session) {
12323e230dd2SCorentin Chary         ret = gnutls_read(vs->tls.session, data, datalen);
12333e230dd2SCorentin Chary         if (ret < 0) {
12343e230dd2SCorentin Chary             if (ret == GNUTLS_E_AGAIN)
12353e230dd2SCorentin Chary                 errno = EAGAIN;
12363e230dd2SCorentin Chary             else
12373e230dd2SCorentin Chary                 errno = EIO;
12383e230dd2SCorentin Chary             ret = -1;
12393e230dd2SCorentin Chary         }
12403e230dd2SCorentin Chary     } else
12413e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
12423e230dd2SCorentin Chary         ret = recv(vs->csock, (void *)data, datalen, 0);
12433e230dd2SCorentin Chary     VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret);
12443e230dd2SCorentin Chary     return vnc_client_io_error(vs, ret, socket_error());
12453e230dd2SCorentin Chary }
12463e230dd2SCorentin Chary 
12473e230dd2SCorentin Chary 
12483e230dd2SCorentin Chary /*
12493e230dd2SCorentin Chary  * Called to read data from the client socket to the input buffer,
12503e230dd2SCorentin Chary  * when not using any SASL SSF encryption layers. Will read as much
12513e230dd2SCorentin Chary  * data as possible without blocking.
12523e230dd2SCorentin Chary  *
12533e230dd2SCorentin Chary  * Returns the number of bytes read. Returns -1 on error, and
12543e230dd2SCorentin Chary  * disconnects the client socket.
12553e230dd2SCorentin Chary  */
12563e230dd2SCorentin Chary static long vnc_client_read_plain(VncState *vs)
12573e230dd2SCorentin Chary {
12583e230dd2SCorentin Chary     int ret;
12593e230dd2SCorentin Chary     VNC_DEBUG("Read plain %p size %zd offset %zd\n",
12603e230dd2SCorentin Chary               vs->input.buffer, vs->input.capacity, vs->input.offset);
12613e230dd2SCorentin Chary     buffer_reserve(&vs->input, 4096);
12623e230dd2SCorentin Chary     ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096);
12633e230dd2SCorentin Chary     if (!ret)
12643e230dd2SCorentin Chary         return 0;
12653e230dd2SCorentin Chary     vs->input.offset += ret;
12663e230dd2SCorentin Chary     return ret;
12673e230dd2SCorentin Chary }
12683e230dd2SCorentin Chary 
12693e230dd2SCorentin Chary 
12703e230dd2SCorentin Chary /*
12713e230dd2SCorentin Chary  * First function called whenever there is more data to be read from
12723e230dd2SCorentin Chary  * the client socket. Will delegate actual work according to whether
12733e230dd2SCorentin Chary  * SASL SSF layers are enabled (thus requiring decryption calls)
12743e230dd2SCorentin Chary  */
12753e230dd2SCorentin Chary void vnc_client_read(void *opaque)
12763e230dd2SCorentin Chary {
12773e230dd2SCorentin Chary     VncState *vs = opaque;
12783e230dd2SCorentin Chary     long ret;
12793e230dd2SCorentin Chary 
12803e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
12813e230dd2SCorentin Chary     if (vs->sasl.conn && vs->sasl.runSSF)
12823e230dd2SCorentin Chary         ret = vnc_client_read_sasl(vs);
12833e230dd2SCorentin Chary     else
12843e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
12853e230dd2SCorentin Chary         ret = vnc_client_read_plain(vs);
12863e230dd2SCorentin Chary     if (!ret) {
12873e230dd2SCorentin Chary         if (vs->csock == -1)
12883e230dd2SCorentin Chary             vnc_disconnect_finish(vs);
12893e230dd2SCorentin Chary         return;
12903e230dd2SCorentin Chary     }
12913e230dd2SCorentin Chary 
12923e230dd2SCorentin Chary     while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
12933e230dd2SCorentin Chary         size_t len = vs->read_handler_expect;
12943e230dd2SCorentin Chary         int ret;
12953e230dd2SCorentin Chary 
12963e230dd2SCorentin Chary         ret = vs->read_handler(vs, vs->input.buffer, len);
12973e230dd2SCorentin Chary         if (vs->csock == -1) {
12983e230dd2SCorentin Chary             vnc_disconnect_finish(vs);
12993e230dd2SCorentin Chary             return;
13003e230dd2SCorentin Chary         }
13013e230dd2SCorentin Chary 
13023e230dd2SCorentin Chary         if (!ret) {
13033e230dd2SCorentin Chary             memmove(vs->input.buffer, vs->input.buffer + len, (vs->input.offset - len));
13043e230dd2SCorentin Chary             vs->input.offset -= len;
13053e230dd2SCorentin Chary         } else {
13063e230dd2SCorentin Chary             vs->read_handler_expect = ret;
13073e230dd2SCorentin Chary         }
13083e230dd2SCorentin Chary     }
13093e230dd2SCorentin Chary }
13103e230dd2SCorentin Chary 
13113e230dd2SCorentin Chary void vnc_write(VncState *vs, const void *data, size_t len)
13123e230dd2SCorentin Chary {
13133e230dd2SCorentin Chary     buffer_reserve(&vs->output, len);
13143e230dd2SCorentin Chary 
13153e230dd2SCorentin Chary     if (vs->csock != -1 && buffer_empty(&vs->output)) {
13163e230dd2SCorentin Chary         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
13173e230dd2SCorentin Chary     }
13183e230dd2SCorentin Chary 
13193e230dd2SCorentin Chary     buffer_append(&vs->output, data, len);
13203e230dd2SCorentin Chary }
13213e230dd2SCorentin Chary 
13223e230dd2SCorentin Chary void vnc_write_s32(VncState *vs, int32_t value)
13233e230dd2SCorentin Chary {
13243e230dd2SCorentin Chary     vnc_write_u32(vs, *(uint32_t *)&value);
13253e230dd2SCorentin Chary }
13263e230dd2SCorentin Chary 
13273e230dd2SCorentin Chary void vnc_write_u32(VncState *vs, uint32_t value)
13283e230dd2SCorentin Chary {
13293e230dd2SCorentin Chary     uint8_t buf[4];
13303e230dd2SCorentin Chary 
13313e230dd2SCorentin Chary     buf[0] = (value >> 24) & 0xFF;
13323e230dd2SCorentin Chary     buf[1] = (value >> 16) & 0xFF;
13333e230dd2SCorentin Chary     buf[2] = (value >>  8) & 0xFF;
13343e230dd2SCorentin Chary     buf[3] = value & 0xFF;
13353e230dd2SCorentin Chary 
13363e230dd2SCorentin Chary     vnc_write(vs, buf, 4);
13373e230dd2SCorentin Chary }
13383e230dd2SCorentin Chary 
13393e230dd2SCorentin Chary void vnc_write_u16(VncState *vs, uint16_t value)
13403e230dd2SCorentin Chary {
13413e230dd2SCorentin Chary     uint8_t buf[2];
13423e230dd2SCorentin Chary 
13433e230dd2SCorentin Chary     buf[0] = (value >> 8) & 0xFF;
13443e230dd2SCorentin Chary     buf[1] = value & 0xFF;
13453e230dd2SCorentin Chary 
13463e230dd2SCorentin Chary     vnc_write(vs, buf, 2);
13473e230dd2SCorentin Chary }
13483e230dd2SCorentin Chary 
13493e230dd2SCorentin Chary void vnc_write_u8(VncState *vs, uint8_t value)
13503e230dd2SCorentin Chary {
13513e230dd2SCorentin Chary     vnc_write(vs, (char *)&value, 1);
13523e230dd2SCorentin Chary }
13533e230dd2SCorentin Chary 
13543e230dd2SCorentin Chary void vnc_flush(VncState *vs)
13553e230dd2SCorentin Chary {
1356bd023f95SCorentin Chary     vnc_lock_output(vs);
1357bd023f95SCorentin Chary     if (vs->csock != -1 && vs->output.offset) {
1358bd023f95SCorentin Chary         vnc_client_write_locked(vs);
1359bd023f95SCorentin Chary     }
1360bd023f95SCorentin Chary     vnc_unlock_output(vs);
13613e230dd2SCorentin Chary }
13623e230dd2SCorentin Chary 
13633e230dd2SCorentin Chary uint8_t read_u8(uint8_t *data, size_t offset)
13643e230dd2SCorentin Chary {
13653e230dd2SCorentin Chary     return data[offset];
13663e230dd2SCorentin Chary }
13673e230dd2SCorentin Chary 
13683e230dd2SCorentin Chary uint16_t read_u16(uint8_t *data, size_t offset)
13693e230dd2SCorentin Chary {
13703e230dd2SCorentin Chary     return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
13713e230dd2SCorentin Chary }
13723e230dd2SCorentin Chary 
13733e230dd2SCorentin Chary int32_t read_s32(uint8_t *data, size_t offset)
13743e230dd2SCorentin Chary {
13753e230dd2SCorentin Chary     return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
13763e230dd2SCorentin Chary                      (data[offset + 2] << 8) | data[offset + 3]);
13773e230dd2SCorentin Chary }
13783e230dd2SCorentin Chary 
13793e230dd2SCorentin Chary uint32_t read_u32(uint8_t *data, size_t offset)
13803e230dd2SCorentin Chary {
13813e230dd2SCorentin Chary     return ((data[offset] << 24) | (data[offset + 1] << 16) |
13823e230dd2SCorentin Chary             (data[offset + 2] << 8) | data[offset + 3]);
13833e230dd2SCorentin Chary }
13843e230dd2SCorentin Chary 
13853e230dd2SCorentin Chary static void client_cut_text(VncState *vs, size_t len, uint8_t *text)
13863e230dd2SCorentin Chary {
13873e230dd2SCorentin Chary }
13883e230dd2SCorentin Chary 
13893e230dd2SCorentin Chary static void check_pointer_type_change(Notifier *notifier)
13903e230dd2SCorentin Chary {
13913e230dd2SCorentin Chary     VncState *vs = container_of(notifier, VncState, mouse_mode_notifier);
13923e230dd2SCorentin Chary     int absolute = kbd_mouse_is_absolute();
13933e230dd2SCorentin Chary 
13943e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) {
1395bd023f95SCorentin Chary         vnc_lock_output(vs);
13963e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
13973e230dd2SCorentin Chary         vnc_write_u8(vs, 0);
13983e230dd2SCorentin Chary         vnc_write_u16(vs, 1);
13993e230dd2SCorentin Chary         vnc_framebuffer_update(vs, absolute, 0,
14003e230dd2SCorentin Chary                                ds_get_width(vs->ds), ds_get_height(vs->ds),
14013e230dd2SCorentin Chary                                VNC_ENCODING_POINTER_TYPE_CHANGE);
1402bd023f95SCorentin Chary         vnc_unlock_output(vs);
14033e230dd2SCorentin Chary         vnc_flush(vs);
14043e230dd2SCorentin Chary     }
14053e230dd2SCorentin Chary     vs->absolute = absolute;
14063e230dd2SCorentin Chary }
14073e230dd2SCorentin Chary 
14083e230dd2SCorentin Chary static void pointer_event(VncState *vs, int button_mask, int x, int y)
14093e230dd2SCorentin Chary {
14103e230dd2SCorentin Chary     int buttons = 0;
14113e230dd2SCorentin Chary     int dz = 0;
14123e230dd2SCorentin Chary 
14133e230dd2SCorentin Chary     if (button_mask & 0x01)
14143e230dd2SCorentin Chary         buttons |= MOUSE_EVENT_LBUTTON;
14153e230dd2SCorentin Chary     if (button_mask & 0x02)
14163e230dd2SCorentin Chary         buttons |= MOUSE_EVENT_MBUTTON;
14173e230dd2SCorentin Chary     if (button_mask & 0x04)
14183e230dd2SCorentin Chary         buttons |= MOUSE_EVENT_RBUTTON;
14193e230dd2SCorentin Chary     if (button_mask & 0x08)
14203e230dd2SCorentin Chary         dz = -1;
14213e230dd2SCorentin Chary     if (button_mask & 0x10)
14223e230dd2SCorentin Chary         dz = 1;
14233e230dd2SCorentin Chary 
14243e230dd2SCorentin Chary     if (vs->absolute) {
14253e230dd2SCorentin Chary         kbd_mouse_event(ds_get_width(vs->ds) > 1 ?
14263e230dd2SCorentin Chary                           x * 0x7FFF / (ds_get_width(vs->ds) - 1) : 0x4000,
14273e230dd2SCorentin Chary                         ds_get_height(vs->ds) > 1 ?
14283e230dd2SCorentin Chary                           y * 0x7FFF / (ds_get_height(vs->ds) - 1) : 0x4000,
14293e230dd2SCorentin Chary                         dz, buttons);
14303e230dd2SCorentin Chary     } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) {
14313e230dd2SCorentin Chary         x -= 0x7FFF;
14323e230dd2SCorentin Chary         y -= 0x7FFF;
14333e230dd2SCorentin Chary 
14343e230dd2SCorentin Chary         kbd_mouse_event(x, y, dz, buttons);
14353e230dd2SCorentin Chary     } else {
14363e230dd2SCorentin Chary         if (vs->last_x != -1)
14373e230dd2SCorentin Chary             kbd_mouse_event(x - vs->last_x,
14383e230dd2SCorentin Chary                             y - vs->last_y,
14393e230dd2SCorentin Chary                             dz, buttons);
14403e230dd2SCorentin Chary         vs->last_x = x;
14413e230dd2SCorentin Chary         vs->last_y = y;
14423e230dd2SCorentin Chary     }
14433e230dd2SCorentin Chary }
14443e230dd2SCorentin Chary 
14453e230dd2SCorentin Chary static void reset_keys(VncState *vs)
14463e230dd2SCorentin Chary {
14473e230dd2SCorentin Chary     int i;
14483e230dd2SCorentin Chary     for(i = 0; i < 256; i++) {
14493e230dd2SCorentin Chary         if (vs->modifiers_state[i]) {
14503e230dd2SCorentin Chary             if (i & SCANCODE_GREY)
14513e230dd2SCorentin Chary                 kbd_put_keycode(SCANCODE_EMUL0);
14523e230dd2SCorentin Chary             kbd_put_keycode(i | SCANCODE_UP);
14533e230dd2SCorentin Chary             vs->modifiers_state[i] = 0;
14543e230dd2SCorentin Chary         }
14553e230dd2SCorentin Chary     }
14563e230dd2SCorentin Chary }
14573e230dd2SCorentin Chary 
14583e230dd2SCorentin Chary static void press_key(VncState *vs, int keysym)
14593e230dd2SCorentin Chary {
14603e230dd2SCorentin Chary     int keycode = keysym2scancode(vs->vd->kbd_layout, keysym) & SCANCODE_KEYMASK;
14613e230dd2SCorentin Chary     if (keycode & SCANCODE_GREY)
14623e230dd2SCorentin Chary         kbd_put_keycode(SCANCODE_EMUL0);
14633e230dd2SCorentin Chary     kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK);
14643e230dd2SCorentin Chary     if (keycode & SCANCODE_GREY)
14653e230dd2SCorentin Chary         kbd_put_keycode(SCANCODE_EMUL0);
14663e230dd2SCorentin Chary     kbd_put_keycode(keycode | SCANCODE_UP);
14673e230dd2SCorentin Chary }
14683e230dd2SCorentin Chary 
14693e230dd2SCorentin Chary static void kbd_leds(void *opaque, int ledstate)
14703e230dd2SCorentin Chary {
14713e230dd2SCorentin Chary     VncState *vs = opaque;
14723e230dd2SCorentin Chary     int caps, num;
14733e230dd2SCorentin Chary 
14743e230dd2SCorentin Chary     caps = ledstate & QEMU_CAPS_LOCK_LED ? 1 : 0;
14753e230dd2SCorentin Chary     num  = ledstate & QEMU_NUM_LOCK_LED  ? 1 : 0;
14763e230dd2SCorentin Chary 
14773e230dd2SCorentin Chary     if (vs->modifiers_state[0x3a] != caps) {
14783e230dd2SCorentin Chary         vs->modifiers_state[0x3a] = caps;
14793e230dd2SCorentin Chary     }
14803e230dd2SCorentin Chary     if (vs->modifiers_state[0x45] != num) {
14813e230dd2SCorentin Chary         vs->modifiers_state[0x45] = num;
14823e230dd2SCorentin Chary     }
14833e230dd2SCorentin Chary }
14843e230dd2SCorentin Chary 
14853e230dd2SCorentin Chary static void do_key_event(VncState *vs, int down, int keycode, int sym)
14863e230dd2SCorentin Chary {
14873e230dd2SCorentin Chary     /* QEMU console switch */
14883e230dd2SCorentin Chary     switch(keycode) {
14893e230dd2SCorentin Chary     case 0x2a:                          /* Left Shift */
14903e230dd2SCorentin Chary     case 0x36:                          /* Right Shift */
14913e230dd2SCorentin Chary     case 0x1d:                          /* Left CTRL */
14923e230dd2SCorentin Chary     case 0x9d:                          /* Right CTRL */
14933e230dd2SCorentin Chary     case 0x38:                          /* Left ALT */
14943e230dd2SCorentin Chary     case 0xb8:                          /* Right ALT */
14953e230dd2SCorentin Chary         if (down)
14963e230dd2SCorentin Chary             vs->modifiers_state[keycode] = 1;
14973e230dd2SCorentin Chary         else
14983e230dd2SCorentin Chary             vs->modifiers_state[keycode] = 0;
14993e230dd2SCorentin Chary         break;
15003e230dd2SCorentin Chary     case 0x02 ... 0x0a: /* '1' to '9' keys */
15013e230dd2SCorentin Chary         if (down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) {
15023e230dd2SCorentin Chary             /* Reset the modifiers sent to the current console */
15033e230dd2SCorentin Chary             reset_keys(vs);
15043e230dd2SCorentin Chary             console_select(keycode - 0x02);
15053e230dd2SCorentin Chary             return;
15063e230dd2SCorentin Chary         }
15073e230dd2SCorentin Chary         break;
15083e230dd2SCorentin Chary     case 0x3a:                        /* CapsLock */
15093e230dd2SCorentin Chary     case 0x45:                        /* NumLock */
15103e230dd2SCorentin Chary         if (down)
15113e230dd2SCorentin Chary             vs->modifiers_state[keycode] ^= 1;
15123e230dd2SCorentin Chary         break;
15133e230dd2SCorentin Chary     }
15143e230dd2SCorentin Chary 
15159892088bSGerd Hoffmann     if (down && vs->vd->lock_key_sync &&
15163e230dd2SCorentin Chary         keycode_is_keypad(vs->vd->kbd_layout, keycode)) {
15173e230dd2SCorentin Chary         /* If the numlock state needs to change then simulate an additional
15183e230dd2SCorentin Chary            keypress before sending this one.  This will happen if the user
15193e230dd2SCorentin Chary            toggles numlock away from the VNC window.
15203e230dd2SCorentin Chary         */
15213e230dd2SCorentin Chary         if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) {
15223e230dd2SCorentin Chary             if (!vs->modifiers_state[0x45]) {
15233e230dd2SCorentin Chary                 vs->modifiers_state[0x45] = 1;
15243e230dd2SCorentin Chary                 press_key(vs, 0xff7f);
15253e230dd2SCorentin Chary             }
15263e230dd2SCorentin Chary         } else {
15273e230dd2SCorentin Chary             if (vs->modifiers_state[0x45]) {
15283e230dd2SCorentin Chary                 vs->modifiers_state[0x45] = 0;
15293e230dd2SCorentin Chary                 press_key(vs, 0xff7f);
15303e230dd2SCorentin Chary             }
15313e230dd2SCorentin Chary         }
15323e230dd2SCorentin Chary     }
15333e230dd2SCorentin Chary 
15349892088bSGerd Hoffmann     if (down && vs->vd->lock_key_sync &&
15353e230dd2SCorentin Chary         ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) {
15363e230dd2SCorentin Chary         /* If the capslock state needs to change then simulate an additional
15373e230dd2SCorentin Chary            keypress before sending this one.  This will happen if the user
15383e230dd2SCorentin Chary            toggles capslock away from the VNC window.
15393e230dd2SCorentin Chary         */
15403e230dd2SCorentin Chary         int uppercase = !!(sym >= 'A' && sym <= 'Z');
15413e230dd2SCorentin Chary         int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]);
15423e230dd2SCorentin Chary         int capslock = !!(vs->modifiers_state[0x3a]);
15433e230dd2SCorentin Chary         if (capslock) {
15443e230dd2SCorentin Chary             if (uppercase == shift) {
15453e230dd2SCorentin Chary                 vs->modifiers_state[0x3a] = 0;
15463e230dd2SCorentin Chary                 press_key(vs, 0xffe5);
15473e230dd2SCorentin Chary             }
15483e230dd2SCorentin Chary         } else {
15493e230dd2SCorentin Chary             if (uppercase != shift) {
15503e230dd2SCorentin Chary                 vs->modifiers_state[0x3a] = 1;
15513e230dd2SCorentin Chary                 press_key(vs, 0xffe5);
15523e230dd2SCorentin Chary             }
15533e230dd2SCorentin Chary         }
15543e230dd2SCorentin Chary     }
15553e230dd2SCorentin Chary 
15563e230dd2SCorentin Chary     if (is_graphic_console()) {
15573e230dd2SCorentin Chary         if (keycode & SCANCODE_GREY)
15583e230dd2SCorentin Chary             kbd_put_keycode(SCANCODE_EMUL0);
15593e230dd2SCorentin Chary         if (down)
15603e230dd2SCorentin Chary             kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK);
15613e230dd2SCorentin Chary         else
15623e230dd2SCorentin Chary             kbd_put_keycode(keycode | SCANCODE_UP);
15633e230dd2SCorentin Chary     } else {
15643e230dd2SCorentin Chary         /* QEMU console emulation */
15653e230dd2SCorentin Chary         if (down) {
15663e230dd2SCorentin Chary             int numlock = vs->modifiers_state[0x45];
15673e230dd2SCorentin Chary             switch (keycode) {
15683e230dd2SCorentin Chary             case 0x2a:                          /* Left Shift */
15693e230dd2SCorentin Chary             case 0x36:                          /* Right Shift */
15703e230dd2SCorentin Chary             case 0x1d:                          /* Left CTRL */
15713e230dd2SCorentin Chary             case 0x9d:                          /* Right CTRL */
15723e230dd2SCorentin Chary             case 0x38:                          /* Left ALT */
15733e230dd2SCorentin Chary             case 0xb8:                          /* Right ALT */
15743e230dd2SCorentin Chary                 break;
15753e230dd2SCorentin Chary             case 0xc8:
15763e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_UP);
15773e230dd2SCorentin Chary                 break;
15783e230dd2SCorentin Chary             case 0xd0:
15793e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_DOWN);
15803e230dd2SCorentin Chary                 break;
15813e230dd2SCorentin Chary             case 0xcb:
15823e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_LEFT);
15833e230dd2SCorentin Chary                 break;
15843e230dd2SCorentin Chary             case 0xcd:
15853e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_RIGHT);
15863e230dd2SCorentin Chary                 break;
15873e230dd2SCorentin Chary             case 0xd3:
15883e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_DELETE);
15893e230dd2SCorentin Chary                 break;
15903e230dd2SCorentin Chary             case 0xc7:
15913e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_HOME);
15923e230dd2SCorentin Chary                 break;
15933e230dd2SCorentin Chary             case 0xcf:
15943e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_END);
15953e230dd2SCorentin Chary                 break;
15963e230dd2SCorentin Chary             case 0xc9:
15973e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_PAGEUP);
15983e230dd2SCorentin Chary                 break;
15993e230dd2SCorentin Chary             case 0xd1:
16003e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_PAGEDOWN);
16013e230dd2SCorentin Chary                 break;
16023e230dd2SCorentin Chary 
16033e230dd2SCorentin Chary             case 0x47:
16043e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME);
16053e230dd2SCorentin Chary                 break;
16063e230dd2SCorentin Chary             case 0x48:
16073e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP);
16083e230dd2SCorentin Chary                 break;
16093e230dd2SCorentin Chary             case 0x49:
16103e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP);
16113e230dd2SCorentin Chary                 break;
16123e230dd2SCorentin Chary             case 0x4b:
16133e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT);
16143e230dd2SCorentin Chary                 break;
16153e230dd2SCorentin Chary             case 0x4c:
16163e230dd2SCorentin Chary                 kbd_put_keysym('5');
16173e230dd2SCorentin Chary                 break;
16183e230dd2SCorentin Chary             case 0x4d:
16193e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT);
16203e230dd2SCorentin Chary                 break;
16213e230dd2SCorentin Chary             case 0x4f:
16223e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '1' : QEMU_KEY_END);
16233e230dd2SCorentin Chary                 break;
16243e230dd2SCorentin Chary             case 0x50:
16253e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN);
16263e230dd2SCorentin Chary                 break;
16273e230dd2SCorentin Chary             case 0x51:
16283e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN);
16293e230dd2SCorentin Chary                 break;
16303e230dd2SCorentin Chary             case 0x52:
16313e230dd2SCorentin Chary                 kbd_put_keysym('0');
16323e230dd2SCorentin Chary                 break;
16333e230dd2SCorentin Chary             case 0x53:
16343e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE);
16353e230dd2SCorentin Chary                 break;
16363e230dd2SCorentin Chary 
16373e230dd2SCorentin Chary             case 0xb5:
16383e230dd2SCorentin Chary                 kbd_put_keysym('/');
16393e230dd2SCorentin Chary                 break;
16403e230dd2SCorentin Chary             case 0x37:
16413e230dd2SCorentin Chary                 kbd_put_keysym('*');
16423e230dd2SCorentin Chary                 break;
16433e230dd2SCorentin Chary             case 0x4a:
16443e230dd2SCorentin Chary                 kbd_put_keysym('-');
16453e230dd2SCorentin Chary                 break;
16463e230dd2SCorentin Chary             case 0x4e:
16473e230dd2SCorentin Chary                 kbd_put_keysym('+');
16483e230dd2SCorentin Chary                 break;
16493e230dd2SCorentin Chary             case 0x9c:
16503e230dd2SCorentin Chary                 kbd_put_keysym('\n');
16513e230dd2SCorentin Chary                 break;
16523e230dd2SCorentin Chary 
16533e230dd2SCorentin Chary             default:
16543e230dd2SCorentin Chary                 kbd_put_keysym(sym);
16553e230dd2SCorentin Chary                 break;
16563e230dd2SCorentin Chary             }
16573e230dd2SCorentin Chary         }
16583e230dd2SCorentin Chary     }
16593e230dd2SCorentin Chary }
16603e230dd2SCorentin Chary 
16613e230dd2SCorentin Chary static void key_event(VncState *vs, int down, uint32_t sym)
16623e230dd2SCorentin Chary {
16633e230dd2SCorentin Chary     int keycode;
16643e230dd2SCorentin Chary     int lsym = sym;
16653e230dd2SCorentin Chary 
16663e230dd2SCorentin Chary     if (lsym >= 'A' && lsym <= 'Z' && is_graphic_console()) {
16673e230dd2SCorentin Chary         lsym = lsym - 'A' + 'a';
16683e230dd2SCorentin Chary     }
16693e230dd2SCorentin Chary 
16703e230dd2SCorentin Chary     keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF) & SCANCODE_KEYMASK;
16713e230dd2SCorentin Chary     do_key_event(vs, down, keycode, sym);
16723e230dd2SCorentin Chary }
16733e230dd2SCorentin Chary 
16743e230dd2SCorentin Chary static void ext_key_event(VncState *vs, int down,
16753e230dd2SCorentin Chary                           uint32_t sym, uint16_t keycode)
16763e230dd2SCorentin Chary {
16773e230dd2SCorentin Chary     /* if the user specifies a keyboard layout, always use it */
16783e230dd2SCorentin Chary     if (keyboard_layout)
16793e230dd2SCorentin Chary         key_event(vs, down, sym);
16803e230dd2SCorentin Chary     else
16813e230dd2SCorentin Chary         do_key_event(vs, down, keycode, sym);
16823e230dd2SCorentin Chary }
16833e230dd2SCorentin Chary 
16843e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental,
16853e230dd2SCorentin Chary                                        int x_position, int y_position,
16863e230dd2SCorentin Chary                                        int w, int h)
16873e230dd2SCorentin Chary {
16883e230dd2SCorentin Chary     if (y_position > ds_get_height(vs->ds))
16893e230dd2SCorentin Chary         y_position = ds_get_height(vs->ds);
16903e230dd2SCorentin Chary     if (y_position + h >= ds_get_height(vs->ds))
16913e230dd2SCorentin Chary         h = ds_get_height(vs->ds) - y_position;
16923e230dd2SCorentin Chary 
16933e230dd2SCorentin Chary     int i;
16943e230dd2SCorentin Chary     vs->need_update = 1;
16953e230dd2SCorentin Chary     if (!incremental) {
16963e230dd2SCorentin Chary         vs->force_update = 1;
16973e230dd2SCorentin Chary         for (i = 0; i < h; i++) {
16983e230dd2SCorentin Chary             vnc_set_bits(vs->dirty[y_position + i],
16993e230dd2SCorentin Chary                          (ds_get_width(vs->ds) / 16), VNC_DIRTY_WORDS);
17003e230dd2SCorentin Chary         }
17013e230dd2SCorentin Chary     }
17023e230dd2SCorentin Chary }
17033e230dd2SCorentin Chary 
17043e230dd2SCorentin Chary static void send_ext_key_event_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_EXT_KEY_EVENT);
1712bd023f95SCorentin Chary     vnc_unlock_output(vs);
17133e230dd2SCorentin Chary     vnc_flush(vs);
17143e230dd2SCorentin Chary }
17153e230dd2SCorentin Chary 
17163e230dd2SCorentin Chary static void send_ext_audio_ack(VncState *vs)
17173e230dd2SCorentin Chary {
1718bd023f95SCorentin Chary     vnc_lock_output(vs);
17193e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
17203e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
17213e230dd2SCorentin Chary     vnc_write_u16(vs, 1);
17223e230dd2SCorentin Chary     vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds), ds_get_height(vs->ds),
17233e230dd2SCorentin Chary                            VNC_ENCODING_AUDIO);
1724bd023f95SCorentin Chary     vnc_unlock_output(vs);
17253e230dd2SCorentin Chary     vnc_flush(vs);
17263e230dd2SCorentin Chary }
17273e230dd2SCorentin Chary 
17283e230dd2SCorentin Chary static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
17293e230dd2SCorentin Chary {
17303e230dd2SCorentin Chary     int i;
17313e230dd2SCorentin Chary     unsigned int enc = 0;
17323e230dd2SCorentin Chary 
17333e230dd2SCorentin Chary     vs->features = 0;
17343e230dd2SCorentin Chary     vs->vnc_encoding = 0;
1735d1af0e05SCorentin Chary     vs->tight.compression = 9;
1736d1af0e05SCorentin Chary     vs->tight.quality = -1; /* Lossless by default */
17373e230dd2SCorentin Chary     vs->absolute = -1;
17383e230dd2SCorentin Chary 
17393e230dd2SCorentin Chary     /*
17403e230dd2SCorentin Chary      * Start from the end because the encodings are sent in order of preference.
17413e230dd2SCorentin Chary      * This way the prefered encoding (first encoding defined in the array)
17423e230dd2SCorentin Chary      * will be set at the end of the loop.
17433e230dd2SCorentin Chary      */
17443e230dd2SCorentin Chary     for (i = n_encodings - 1; i >= 0; i--) {
17453e230dd2SCorentin Chary         enc = encodings[i];
17463e230dd2SCorentin Chary         switch (enc) {
17473e230dd2SCorentin Chary         case VNC_ENCODING_RAW:
17483e230dd2SCorentin Chary             vs->vnc_encoding = enc;
17493e230dd2SCorentin Chary             break;
17503e230dd2SCorentin Chary         case VNC_ENCODING_COPYRECT:
17513e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_COPYRECT_MASK;
17523e230dd2SCorentin Chary             break;
17533e230dd2SCorentin Chary         case VNC_ENCODING_HEXTILE:
17543e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_HEXTILE_MASK;
17553e230dd2SCorentin Chary             vs->vnc_encoding = enc;
17563e230dd2SCorentin Chary             break;
17573e230dd2SCorentin Chary         case VNC_ENCODING_TIGHT:
17583e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_TIGHT_MASK;
17593e230dd2SCorentin Chary             vs->vnc_encoding = enc;
17603e230dd2SCorentin Chary             break;
1761efe556adSCorentin Chary         case VNC_ENCODING_TIGHT_PNG:
1762efe556adSCorentin Chary             vs->features |= VNC_FEATURE_TIGHT_PNG_MASK;
1763efe556adSCorentin Chary             vs->vnc_encoding = enc;
1764efe556adSCorentin Chary             break;
17653e230dd2SCorentin Chary         case VNC_ENCODING_ZLIB:
17663e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_ZLIB_MASK;
17673e230dd2SCorentin Chary             vs->vnc_encoding = enc;
17683e230dd2SCorentin Chary             break;
17693e230dd2SCorentin Chary         case VNC_ENCODING_DESKTOPRESIZE:
17703e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_RESIZE_MASK;
17713e230dd2SCorentin Chary             break;
17723e230dd2SCorentin Chary         case VNC_ENCODING_POINTER_TYPE_CHANGE:
17733e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK;
17743e230dd2SCorentin Chary             break;
17753e230dd2SCorentin Chary         case VNC_ENCODING_RICH_CURSOR:
17763e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_RICH_CURSOR_MASK;
17773e230dd2SCorentin Chary             break;
17783e230dd2SCorentin Chary         case VNC_ENCODING_EXT_KEY_EVENT:
17793e230dd2SCorentin Chary             send_ext_key_event_ack(vs);
17803e230dd2SCorentin Chary             break;
17813e230dd2SCorentin Chary         case VNC_ENCODING_AUDIO:
17823e230dd2SCorentin Chary             send_ext_audio_ack(vs);
17833e230dd2SCorentin Chary             break;
17843e230dd2SCorentin Chary         case VNC_ENCODING_WMVi:
17853e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_WMVI_MASK;
17863e230dd2SCorentin Chary             break;
17873e230dd2SCorentin Chary         case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9:
1788d1af0e05SCorentin Chary             vs->tight.compression = (enc & 0x0F);
17893e230dd2SCorentin Chary             break;
17903e230dd2SCorentin Chary         case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9:
1791b31f519eSCorentin Chary             if (vs->vd->lossy) {
1792d1af0e05SCorentin Chary                 vs->tight.quality = (enc & 0x0F);
1793b31f519eSCorentin Chary             }
17943e230dd2SCorentin Chary             break;
17953e230dd2SCorentin Chary         default:
17963e230dd2SCorentin Chary             VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc);
17973e230dd2SCorentin Chary             break;
17983e230dd2SCorentin Chary         }
17993e230dd2SCorentin Chary     }
18003e230dd2SCorentin Chary     vnc_desktop_resize(vs);
18013e230dd2SCorentin Chary     check_pointer_type_change(&vs->mouse_mode_notifier);
18023e230dd2SCorentin Chary }
18033e230dd2SCorentin Chary 
18043e230dd2SCorentin Chary static void set_pixel_conversion(VncState *vs)
18053e230dd2SCorentin Chary {
18063e230dd2SCorentin Chary     if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
18073e230dd2SCorentin Chary         (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG) &&
18083e230dd2SCorentin Chary         !memcmp(&(vs->clientds.pf), &(vs->ds->surface->pf), sizeof(PixelFormat))) {
18093e230dd2SCorentin Chary         vs->write_pixels = vnc_write_pixels_copy;
18103e230dd2SCorentin Chary         vnc_hextile_set_pixel_conversion(vs, 0);
18113e230dd2SCorentin Chary     } else {
18123e230dd2SCorentin Chary         vs->write_pixels = vnc_write_pixels_generic;
18133e230dd2SCorentin Chary         vnc_hextile_set_pixel_conversion(vs, 1);
18143e230dd2SCorentin Chary     }
18153e230dd2SCorentin Chary }
18163e230dd2SCorentin Chary 
18173e230dd2SCorentin Chary static void set_pixel_format(VncState *vs,
18183e230dd2SCorentin Chary                              int bits_per_pixel, int depth,
18193e230dd2SCorentin Chary                              int big_endian_flag, int true_color_flag,
18203e230dd2SCorentin Chary                              int red_max, int green_max, int blue_max,
18213e230dd2SCorentin Chary                              int red_shift, int green_shift, int blue_shift)
18223e230dd2SCorentin Chary {
18233e230dd2SCorentin Chary     if (!true_color_flag) {
18243e230dd2SCorentin Chary         vnc_client_error(vs);
18253e230dd2SCorentin Chary         return;
18263e230dd2SCorentin Chary     }
18273e230dd2SCorentin Chary 
18283e230dd2SCorentin Chary     vs->clientds = *(vs->vd->guest.ds);
18293e230dd2SCorentin Chary     vs->clientds.pf.rmax = red_max;
18303e230dd2SCorentin Chary     count_bits(vs->clientds.pf.rbits, red_max);
18313e230dd2SCorentin Chary     vs->clientds.pf.rshift = red_shift;
18323e230dd2SCorentin Chary     vs->clientds.pf.rmask = red_max << red_shift;
18333e230dd2SCorentin Chary     vs->clientds.pf.gmax = green_max;
18343e230dd2SCorentin Chary     count_bits(vs->clientds.pf.gbits, green_max);
18353e230dd2SCorentin Chary     vs->clientds.pf.gshift = green_shift;
18363e230dd2SCorentin Chary     vs->clientds.pf.gmask = green_max << green_shift;
18373e230dd2SCorentin Chary     vs->clientds.pf.bmax = blue_max;
18383e230dd2SCorentin Chary     count_bits(vs->clientds.pf.bbits, blue_max);
18393e230dd2SCorentin Chary     vs->clientds.pf.bshift = blue_shift;
18403e230dd2SCorentin Chary     vs->clientds.pf.bmask = blue_max << blue_shift;
18413e230dd2SCorentin Chary     vs->clientds.pf.bits_per_pixel = bits_per_pixel;
18423e230dd2SCorentin Chary     vs->clientds.pf.bytes_per_pixel = bits_per_pixel / 8;
18433e230dd2SCorentin Chary     vs->clientds.pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel;
18443e230dd2SCorentin Chary     vs->clientds.flags = big_endian_flag ? QEMU_BIG_ENDIAN_FLAG : 0x00;
18453e230dd2SCorentin Chary 
18463e230dd2SCorentin Chary     set_pixel_conversion(vs);
18473e230dd2SCorentin Chary 
18483e230dd2SCorentin Chary     vga_hw_invalidate();
18493e230dd2SCorentin Chary     vga_hw_update();
18503e230dd2SCorentin Chary }
18513e230dd2SCorentin Chary 
18523e230dd2SCorentin Chary static void pixel_format_message (VncState *vs) {
18533e230dd2SCorentin Chary     char pad[3] = { 0, 0, 0 };
18543e230dd2SCorentin Chary 
18553e230dd2SCorentin Chary     vnc_write_u8(vs, vs->ds->surface->pf.bits_per_pixel); /* bits-per-pixel */
18563e230dd2SCorentin Chary     vnc_write_u8(vs, vs->ds->surface->pf.depth); /* depth */
18573e230dd2SCorentin Chary 
18583e230dd2SCorentin Chary #ifdef HOST_WORDS_BIGENDIAN
18593e230dd2SCorentin Chary     vnc_write_u8(vs, 1);             /* big-endian-flag */
18603e230dd2SCorentin Chary #else
18613e230dd2SCorentin Chary     vnc_write_u8(vs, 0);             /* big-endian-flag */
18623e230dd2SCorentin Chary #endif
18633e230dd2SCorentin Chary     vnc_write_u8(vs, 1);             /* true-color-flag */
18643e230dd2SCorentin Chary     vnc_write_u16(vs, vs->ds->surface->pf.rmax);     /* red-max */
18653e230dd2SCorentin Chary     vnc_write_u16(vs, vs->ds->surface->pf.gmax);     /* green-max */
18663e230dd2SCorentin Chary     vnc_write_u16(vs, vs->ds->surface->pf.bmax);     /* blue-max */
18673e230dd2SCorentin Chary     vnc_write_u8(vs, vs->ds->surface->pf.rshift);    /* red-shift */
18683e230dd2SCorentin Chary     vnc_write_u8(vs, vs->ds->surface->pf.gshift);    /* green-shift */
18693e230dd2SCorentin Chary     vnc_write_u8(vs, vs->ds->surface->pf.bshift);    /* blue-shift */
18703e230dd2SCorentin Chary 
18713e230dd2SCorentin Chary     vnc_hextile_set_pixel_conversion(vs, 0);
18723e230dd2SCorentin Chary 
18733e230dd2SCorentin Chary     vs->clientds = *(vs->ds->surface);
18743e230dd2SCorentin Chary     vs->clientds.flags &= ~QEMU_ALLOCATED_FLAG;
18753e230dd2SCorentin Chary     vs->write_pixels = vnc_write_pixels_copy;
18763e230dd2SCorentin Chary 
18773e230dd2SCorentin Chary     vnc_write(vs, pad, 3);           /* padding */
18783e230dd2SCorentin Chary }
18793e230dd2SCorentin Chary 
18803e230dd2SCorentin Chary static void vnc_dpy_setdata(DisplayState *ds)
18813e230dd2SCorentin Chary {
18823e230dd2SCorentin Chary     /* We don't have to do anything */
18833e230dd2SCorentin Chary }
18843e230dd2SCorentin Chary 
18853e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs)
18863e230dd2SCorentin Chary {
18873e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) {
18883e230dd2SCorentin Chary         /* Sending a WMVi message to notify the client*/
1889bd023f95SCorentin Chary         vnc_lock_output(vs);
18903e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
18913e230dd2SCorentin Chary         vnc_write_u8(vs, 0);
18923e230dd2SCorentin Chary         vnc_write_u16(vs, 1); /* number of rects */
18933e230dd2SCorentin Chary         vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds),
18943e230dd2SCorentin Chary                                ds_get_height(vs->ds), VNC_ENCODING_WMVi);
18953e230dd2SCorentin Chary         pixel_format_message(vs);
1896bd023f95SCorentin Chary         vnc_unlock_output(vs);
18973e230dd2SCorentin Chary         vnc_flush(vs);
18983e230dd2SCorentin Chary     } else {
18993e230dd2SCorentin Chary         set_pixel_conversion(vs);
19003e230dd2SCorentin Chary     }
19013e230dd2SCorentin Chary }
19023e230dd2SCorentin Chary 
19033e230dd2SCorentin Chary static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len)
19043e230dd2SCorentin Chary {
19053e230dd2SCorentin Chary     int i;
19063e230dd2SCorentin Chary     uint16_t limit;
19073e230dd2SCorentin Chary     VncDisplay *vd = vs->vd;
19083e230dd2SCorentin Chary 
19093e230dd2SCorentin Chary     if (data[0] > 3) {
19103e230dd2SCorentin Chary         vd->timer_interval = VNC_REFRESH_INTERVAL_BASE;
19113e230dd2SCorentin Chary         if (!qemu_timer_expired(vd->timer, qemu_get_clock(rt_clock) + vd->timer_interval))
19123e230dd2SCorentin Chary             qemu_mod_timer(vd->timer, qemu_get_clock(rt_clock) + vd->timer_interval);
19133e230dd2SCorentin Chary     }
19143e230dd2SCorentin Chary 
19153e230dd2SCorentin Chary     switch (data[0]) {
19163e230dd2SCorentin Chary     case VNC_MSG_CLIENT_SET_PIXEL_FORMAT:
19173e230dd2SCorentin Chary         if (len == 1)
19183e230dd2SCorentin Chary             return 20;
19193e230dd2SCorentin Chary 
19203e230dd2SCorentin Chary         set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5),
19213e230dd2SCorentin Chary                          read_u8(data, 6), read_u8(data, 7),
19223e230dd2SCorentin Chary                          read_u16(data, 8), read_u16(data, 10),
19233e230dd2SCorentin Chary                          read_u16(data, 12), read_u8(data, 14),
19243e230dd2SCorentin Chary                          read_u8(data, 15), read_u8(data, 16));
19253e230dd2SCorentin Chary         break;
19263e230dd2SCorentin Chary     case VNC_MSG_CLIENT_SET_ENCODINGS:
19273e230dd2SCorentin Chary         if (len == 1)
19283e230dd2SCorentin Chary             return 4;
19293e230dd2SCorentin Chary 
19303e230dd2SCorentin Chary         if (len == 4) {
19313e230dd2SCorentin Chary             limit = read_u16(data, 2);
19323e230dd2SCorentin Chary             if (limit > 0)
19333e230dd2SCorentin Chary                 return 4 + (limit * 4);
19343e230dd2SCorentin Chary         } else
19353e230dd2SCorentin Chary             limit = read_u16(data, 2);
19363e230dd2SCorentin Chary 
19373e230dd2SCorentin Chary         for (i = 0; i < limit; i++) {
19383e230dd2SCorentin Chary             int32_t val = read_s32(data, 4 + (i * 4));
19393e230dd2SCorentin Chary             memcpy(data + 4 + (i * 4), &val, sizeof(val));
19403e230dd2SCorentin Chary         }
19413e230dd2SCorentin Chary 
19423e230dd2SCorentin Chary         set_encodings(vs, (int32_t *)(data + 4), limit);
19433e230dd2SCorentin Chary         break;
19443e230dd2SCorentin Chary     case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST:
19453e230dd2SCorentin Chary         if (len == 1)
19463e230dd2SCorentin Chary             return 10;
19473e230dd2SCorentin Chary 
19483e230dd2SCorentin Chary         framebuffer_update_request(vs,
19493e230dd2SCorentin Chary                                    read_u8(data, 1), read_u16(data, 2), read_u16(data, 4),
19503e230dd2SCorentin Chary                                    read_u16(data, 6), read_u16(data, 8));
19513e230dd2SCorentin Chary         break;
19523e230dd2SCorentin Chary     case VNC_MSG_CLIENT_KEY_EVENT:
19533e230dd2SCorentin Chary         if (len == 1)
19543e230dd2SCorentin Chary             return 8;
19553e230dd2SCorentin Chary 
19563e230dd2SCorentin Chary         key_event(vs, read_u8(data, 1), read_u32(data, 4));
19573e230dd2SCorentin Chary         break;
19583e230dd2SCorentin Chary     case VNC_MSG_CLIENT_POINTER_EVENT:
19593e230dd2SCorentin Chary         if (len == 1)
19603e230dd2SCorentin Chary             return 6;
19613e230dd2SCorentin Chary 
19623e230dd2SCorentin Chary         pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
19633e230dd2SCorentin Chary         break;
19643e230dd2SCorentin Chary     case VNC_MSG_CLIENT_CUT_TEXT:
19653e230dd2SCorentin Chary         if (len == 1)
19663e230dd2SCorentin Chary             return 8;
19673e230dd2SCorentin Chary 
19683e230dd2SCorentin Chary         if (len == 8) {
19693e230dd2SCorentin Chary             uint32_t dlen = read_u32(data, 4);
19703e230dd2SCorentin Chary             if (dlen > 0)
19713e230dd2SCorentin Chary                 return 8 + dlen;
19723e230dd2SCorentin Chary         }
19733e230dd2SCorentin Chary 
19743e230dd2SCorentin Chary         client_cut_text(vs, read_u32(data, 4), data + 8);
19753e230dd2SCorentin Chary         break;
19763e230dd2SCorentin Chary     case VNC_MSG_CLIENT_QEMU:
19773e230dd2SCorentin Chary         if (len == 1)
19783e230dd2SCorentin Chary             return 2;
19793e230dd2SCorentin Chary 
19803e230dd2SCorentin Chary         switch (read_u8(data, 1)) {
19813e230dd2SCorentin Chary         case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT:
19823e230dd2SCorentin Chary             if (len == 2)
19833e230dd2SCorentin Chary                 return 12;
19843e230dd2SCorentin Chary 
19853e230dd2SCorentin Chary             ext_key_event(vs, read_u16(data, 2),
19863e230dd2SCorentin Chary                           read_u32(data, 4), read_u32(data, 8));
19873e230dd2SCorentin Chary             break;
19883e230dd2SCorentin Chary         case VNC_MSG_CLIENT_QEMU_AUDIO:
19893e230dd2SCorentin Chary             if (len == 2)
19903e230dd2SCorentin Chary                 return 4;
19913e230dd2SCorentin Chary 
19923e230dd2SCorentin Chary             switch (read_u16 (data, 2)) {
19933e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE:
19943e230dd2SCorentin Chary                 audio_add(vs);
19953e230dd2SCorentin Chary                 break;
19963e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE:
19973e230dd2SCorentin Chary                 audio_del(vs);
19983e230dd2SCorentin Chary                 break;
19993e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT:
20003e230dd2SCorentin Chary                 if (len == 4)
20013e230dd2SCorentin Chary                     return 10;
20023e230dd2SCorentin Chary                 switch (read_u8(data, 4)) {
20033e230dd2SCorentin Chary                 case 0: vs->as.fmt = AUD_FMT_U8; break;
20043e230dd2SCorentin Chary                 case 1: vs->as.fmt = AUD_FMT_S8; break;
20053e230dd2SCorentin Chary                 case 2: vs->as.fmt = AUD_FMT_U16; break;
20063e230dd2SCorentin Chary                 case 3: vs->as.fmt = AUD_FMT_S16; break;
20073e230dd2SCorentin Chary                 case 4: vs->as.fmt = AUD_FMT_U32; break;
20083e230dd2SCorentin Chary                 case 5: vs->as.fmt = AUD_FMT_S32; break;
20093e230dd2SCorentin Chary                 default:
20103e230dd2SCorentin Chary                     printf("Invalid audio format %d\n", read_u8(data, 4));
20113e230dd2SCorentin Chary                     vnc_client_error(vs);
20123e230dd2SCorentin Chary                     break;
20133e230dd2SCorentin Chary                 }
20143e230dd2SCorentin Chary                 vs->as.nchannels = read_u8(data, 5);
20153e230dd2SCorentin Chary                 if (vs->as.nchannels != 1 && vs->as.nchannels != 2) {
20163e230dd2SCorentin Chary                     printf("Invalid audio channel coount %d\n",
20173e230dd2SCorentin Chary                            read_u8(data, 5));
20183e230dd2SCorentin Chary                     vnc_client_error(vs);
20193e230dd2SCorentin Chary                     break;
20203e230dd2SCorentin Chary                 }
20213e230dd2SCorentin Chary                 vs->as.freq = read_u32(data, 6);
20223e230dd2SCorentin Chary                 break;
20233e230dd2SCorentin Chary             default:
20243e230dd2SCorentin Chary                 printf ("Invalid audio message %d\n", read_u8(data, 4));
20253e230dd2SCorentin Chary                 vnc_client_error(vs);
20263e230dd2SCorentin Chary                 break;
20273e230dd2SCorentin Chary             }
20283e230dd2SCorentin Chary             break;
20293e230dd2SCorentin Chary 
20303e230dd2SCorentin Chary         default:
20313e230dd2SCorentin Chary             printf("Msg: %d\n", read_u16(data, 0));
20323e230dd2SCorentin Chary             vnc_client_error(vs);
20333e230dd2SCorentin Chary             break;
20343e230dd2SCorentin Chary         }
20353e230dd2SCorentin Chary         break;
20363e230dd2SCorentin Chary     default:
20373e230dd2SCorentin Chary         printf("Msg: %d\n", data[0]);
20383e230dd2SCorentin Chary         vnc_client_error(vs);
20393e230dd2SCorentin Chary         break;
20403e230dd2SCorentin Chary     }
20413e230dd2SCorentin Chary 
20423e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_msg, 1);
20433e230dd2SCorentin Chary     return 0;
20443e230dd2SCorentin Chary }
20453e230dd2SCorentin Chary 
20463e230dd2SCorentin Chary static int protocol_client_init(VncState *vs, uint8_t *data, size_t len)
20473e230dd2SCorentin Chary {
20483e230dd2SCorentin Chary     char buf[1024];
20493e230dd2SCorentin Chary     int size;
20503e230dd2SCorentin Chary 
20513e230dd2SCorentin Chary     vs->client_width = ds_get_width(vs->ds);
20523e230dd2SCorentin Chary     vs->client_height = ds_get_height(vs->ds);
20533e230dd2SCorentin Chary     vnc_write_u16(vs, vs->client_width);
20543e230dd2SCorentin Chary     vnc_write_u16(vs, vs->client_height);
20553e230dd2SCorentin Chary 
20563e230dd2SCorentin Chary     pixel_format_message(vs);
20573e230dd2SCorentin Chary 
20583e230dd2SCorentin Chary     if (qemu_name)
20593e230dd2SCorentin Chary         size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name);
20603e230dd2SCorentin Chary     else
20613e230dd2SCorentin Chary         size = snprintf(buf, sizeof(buf), "QEMU");
20623e230dd2SCorentin Chary 
20633e230dd2SCorentin Chary     vnc_write_u32(vs, size);
20643e230dd2SCorentin Chary     vnc_write(vs, buf, size);
20653e230dd2SCorentin Chary     vnc_flush(vs);
20663e230dd2SCorentin Chary 
20673e230dd2SCorentin Chary     vnc_client_cache_auth(vs);
20683e230dd2SCorentin Chary     vnc_qmp_event(vs, QEVENT_VNC_INITIALIZED);
20693e230dd2SCorentin Chary 
20703e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_msg, 1);
20713e230dd2SCorentin Chary 
20723e230dd2SCorentin Chary     return 0;
20733e230dd2SCorentin Chary }
20743e230dd2SCorentin Chary 
20753e230dd2SCorentin Chary void start_client_init(VncState *vs)
20763e230dd2SCorentin Chary {
20773e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_init, 1);
20783e230dd2SCorentin Chary }
20793e230dd2SCorentin Chary 
20803e230dd2SCorentin Chary static void make_challenge(VncState *vs)
20813e230dd2SCorentin Chary {
20823e230dd2SCorentin Chary     int i;
20833e230dd2SCorentin Chary 
20843e230dd2SCorentin Chary     srand(time(NULL)+getpid()+getpid()*987654+rand());
20853e230dd2SCorentin Chary 
20863e230dd2SCorentin Chary     for (i = 0 ; i < sizeof(vs->challenge) ; i++)
20873e230dd2SCorentin Chary         vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0));
20883e230dd2SCorentin Chary }
20893e230dd2SCorentin Chary 
20903e230dd2SCorentin Chary static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len)
20913e230dd2SCorentin Chary {
20923e230dd2SCorentin Chary     unsigned char response[VNC_AUTH_CHALLENGE_SIZE];
20933e230dd2SCorentin Chary     int i, j, pwlen;
20943e230dd2SCorentin Chary     unsigned char key[8];
20953c9405a0SGerd Hoffmann     time_t now = time(NULL);
20963e230dd2SCorentin Chary 
20971cd20f8bSAnthony Liguori     if (!vs->vd->password) {
20983e230dd2SCorentin Chary         VNC_DEBUG("No password configured on server");
20996bffdf0fSGerd Hoffmann         goto reject;
21003e230dd2SCorentin Chary     }
21013c9405a0SGerd Hoffmann     if (vs->vd->expires < now) {
21023c9405a0SGerd Hoffmann         VNC_DEBUG("Password is expired");
21033c9405a0SGerd Hoffmann         goto reject;
21043c9405a0SGerd Hoffmann     }
21053e230dd2SCorentin Chary 
21063e230dd2SCorentin Chary     memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE);
21073e230dd2SCorentin Chary 
21083e230dd2SCorentin Chary     /* Calculate the expected challenge response */
21093e230dd2SCorentin Chary     pwlen = strlen(vs->vd->password);
21103e230dd2SCorentin Chary     for (i=0; i<sizeof(key); i++)
21113e230dd2SCorentin Chary         key[i] = i<pwlen ? vs->vd->password[i] : 0;
21123e230dd2SCorentin Chary     deskey(key, EN0);
21133e230dd2SCorentin Chary     for (j = 0; j < VNC_AUTH_CHALLENGE_SIZE; j += 8)
21143e230dd2SCorentin Chary         des(response+j, response+j);
21153e230dd2SCorentin Chary 
21163e230dd2SCorentin Chary     /* Compare expected vs actual challenge response */
21173e230dd2SCorentin Chary     if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) {
21183e230dd2SCorentin Chary         VNC_DEBUG("Client challenge reponse did not match\n");
21196bffdf0fSGerd Hoffmann         goto reject;
21206bffdf0fSGerd Hoffmann     } else {
21216bffdf0fSGerd Hoffmann         VNC_DEBUG("Accepting VNC challenge response\n");
21226bffdf0fSGerd Hoffmann         vnc_write_u32(vs, 0); /* Accept auth */
21236bffdf0fSGerd Hoffmann         vnc_flush(vs);
21246bffdf0fSGerd Hoffmann 
21256bffdf0fSGerd Hoffmann         start_client_init(vs);
21266bffdf0fSGerd Hoffmann     }
21276bffdf0fSGerd Hoffmann     return 0;
21286bffdf0fSGerd Hoffmann 
21296bffdf0fSGerd Hoffmann reject:
21303e230dd2SCorentin Chary     vnc_write_u32(vs, 1); /* Reject auth */
21313e230dd2SCorentin Chary     if (vs->minor >= 8) {
21323e230dd2SCorentin Chary         static const char err[] = "Authentication failed";
21333e230dd2SCorentin Chary         vnc_write_u32(vs, sizeof(err));
21343e230dd2SCorentin Chary         vnc_write(vs, err, sizeof(err));
21353e230dd2SCorentin Chary     }
21363e230dd2SCorentin Chary     vnc_flush(vs);
21373e230dd2SCorentin Chary     vnc_client_error(vs);
21383e230dd2SCorentin Chary     return 0;
21393e230dd2SCorentin Chary }
21403e230dd2SCorentin Chary 
21413e230dd2SCorentin Chary void start_auth_vnc(VncState *vs)
21423e230dd2SCorentin Chary {
21433e230dd2SCorentin Chary     make_challenge(vs);
21443e230dd2SCorentin Chary     /* Send client a 'random' challenge */
21453e230dd2SCorentin Chary     vnc_write(vs, vs->challenge, sizeof(vs->challenge));
21463e230dd2SCorentin Chary     vnc_flush(vs);
21473e230dd2SCorentin Chary 
21483e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge));
21493e230dd2SCorentin Chary }
21503e230dd2SCorentin Chary 
21513e230dd2SCorentin Chary 
21523e230dd2SCorentin Chary static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len)
21533e230dd2SCorentin Chary {
21543e230dd2SCorentin Chary     /* We only advertise 1 auth scheme at a time, so client
21553e230dd2SCorentin Chary      * must pick the one we sent. Verify this */
21563e230dd2SCorentin Chary     if (data[0] != vs->vd->auth) { /* Reject auth */
21573e230dd2SCorentin Chary        VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data[0]);
21583e230dd2SCorentin Chary        vnc_write_u32(vs, 1);
21593e230dd2SCorentin Chary        if (vs->minor >= 8) {
21603e230dd2SCorentin Chary            static const char err[] = "Authentication failed";
21613e230dd2SCorentin Chary            vnc_write_u32(vs, sizeof(err));
21623e230dd2SCorentin Chary            vnc_write(vs, err, sizeof(err));
21633e230dd2SCorentin Chary        }
21643e230dd2SCorentin Chary        vnc_client_error(vs);
21653e230dd2SCorentin Chary     } else { /* Accept requested auth */
21663e230dd2SCorentin Chary        VNC_DEBUG("Client requested auth %d\n", (int)data[0]);
21673e230dd2SCorentin Chary        switch (vs->vd->auth) {
21683e230dd2SCorentin Chary        case VNC_AUTH_NONE:
21693e230dd2SCorentin Chary            VNC_DEBUG("Accept auth none\n");
21703e230dd2SCorentin Chary            if (vs->minor >= 8) {
21713e230dd2SCorentin Chary                vnc_write_u32(vs, 0); /* Accept auth completion */
21723e230dd2SCorentin Chary                vnc_flush(vs);
21733e230dd2SCorentin Chary            }
21743e230dd2SCorentin Chary            start_client_init(vs);
21753e230dd2SCorentin Chary            break;
21763e230dd2SCorentin Chary 
21773e230dd2SCorentin Chary        case VNC_AUTH_VNC:
21783e230dd2SCorentin Chary            VNC_DEBUG("Start VNC auth\n");
21793e230dd2SCorentin Chary            start_auth_vnc(vs);
21803e230dd2SCorentin Chary            break;
21813e230dd2SCorentin Chary 
21823e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
21833e230dd2SCorentin Chary        case VNC_AUTH_VENCRYPT:
21843e230dd2SCorentin Chary            VNC_DEBUG("Accept VeNCrypt auth\n");;
21853e230dd2SCorentin Chary            start_auth_vencrypt(vs);
21863e230dd2SCorentin Chary            break;
21873e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
21883e230dd2SCorentin Chary 
21893e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
21903e230dd2SCorentin Chary        case VNC_AUTH_SASL:
21913e230dd2SCorentin Chary            VNC_DEBUG("Accept SASL auth\n");
21923e230dd2SCorentin Chary            start_auth_sasl(vs);
21933e230dd2SCorentin Chary            break;
21943e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
21953e230dd2SCorentin Chary 
21963e230dd2SCorentin Chary        default: /* Should not be possible, but just in case */
21973e230dd2SCorentin Chary            VNC_DEBUG("Reject auth %d server code bug\n", vs->vd->auth);
21983e230dd2SCorentin Chary            vnc_write_u8(vs, 1);
21993e230dd2SCorentin Chary            if (vs->minor >= 8) {
22003e230dd2SCorentin Chary                static const char err[] = "Authentication failed";
22013e230dd2SCorentin Chary                vnc_write_u32(vs, sizeof(err));
22023e230dd2SCorentin Chary                vnc_write(vs, err, sizeof(err));
22033e230dd2SCorentin Chary            }
22043e230dd2SCorentin Chary            vnc_client_error(vs);
22053e230dd2SCorentin Chary        }
22063e230dd2SCorentin Chary     }
22073e230dd2SCorentin Chary     return 0;
22083e230dd2SCorentin Chary }
22093e230dd2SCorentin Chary 
22103e230dd2SCorentin Chary static int protocol_version(VncState *vs, uint8_t *version, size_t len)
22113e230dd2SCorentin Chary {
22123e230dd2SCorentin Chary     char local[13];
22133e230dd2SCorentin Chary 
22143e230dd2SCorentin Chary     memcpy(local, version, 12);
22153e230dd2SCorentin Chary     local[12] = 0;
22163e230dd2SCorentin Chary 
22173e230dd2SCorentin Chary     if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) {
22183e230dd2SCorentin Chary         VNC_DEBUG("Malformed protocol version %s\n", local);
22193e230dd2SCorentin Chary         vnc_client_error(vs);
22203e230dd2SCorentin Chary         return 0;
22213e230dd2SCorentin Chary     }
22223e230dd2SCorentin Chary     VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor);
22233e230dd2SCorentin Chary     if (vs->major != 3 ||
22243e230dd2SCorentin Chary         (vs->minor != 3 &&
22253e230dd2SCorentin Chary          vs->minor != 4 &&
22263e230dd2SCorentin Chary          vs->minor != 5 &&
22273e230dd2SCorentin Chary          vs->minor != 7 &&
22283e230dd2SCorentin Chary          vs->minor != 8)) {
22293e230dd2SCorentin Chary         VNC_DEBUG("Unsupported client version\n");
22303e230dd2SCorentin Chary         vnc_write_u32(vs, VNC_AUTH_INVALID);
22313e230dd2SCorentin Chary         vnc_flush(vs);
22323e230dd2SCorentin Chary         vnc_client_error(vs);
22333e230dd2SCorentin Chary         return 0;
22343e230dd2SCorentin Chary     }
22353e230dd2SCorentin Chary     /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
22363e230dd2SCorentin Chary      * as equivalent to v3.3 by servers
22373e230dd2SCorentin Chary      */
22383e230dd2SCorentin Chary     if (vs->minor == 4 || vs->minor == 5)
22393e230dd2SCorentin Chary         vs->minor = 3;
22403e230dd2SCorentin Chary 
22413e230dd2SCorentin Chary     if (vs->minor == 3) {
22423e230dd2SCorentin Chary         if (vs->vd->auth == VNC_AUTH_NONE) {
22433e230dd2SCorentin Chary             VNC_DEBUG("Tell client auth none\n");
22443e230dd2SCorentin Chary             vnc_write_u32(vs, vs->vd->auth);
22453e230dd2SCorentin Chary             vnc_flush(vs);
22463e230dd2SCorentin Chary             start_client_init(vs);
22473e230dd2SCorentin Chary        } else if (vs->vd->auth == VNC_AUTH_VNC) {
22483e230dd2SCorentin Chary             VNC_DEBUG("Tell client VNC auth\n");
22493e230dd2SCorentin Chary             vnc_write_u32(vs, vs->vd->auth);
22503e230dd2SCorentin Chary             vnc_flush(vs);
22513e230dd2SCorentin Chary             start_auth_vnc(vs);
22523e230dd2SCorentin Chary        } else {
22533e230dd2SCorentin Chary             VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->vd->auth);
22543e230dd2SCorentin Chary             vnc_write_u32(vs, VNC_AUTH_INVALID);
22553e230dd2SCorentin Chary             vnc_flush(vs);
22563e230dd2SCorentin Chary             vnc_client_error(vs);
22573e230dd2SCorentin Chary        }
22583e230dd2SCorentin Chary     } else {
22593e230dd2SCorentin Chary         VNC_DEBUG("Telling client we support auth %d\n", vs->vd->auth);
22603e230dd2SCorentin Chary         vnc_write_u8(vs, 1); /* num auth */
22613e230dd2SCorentin Chary         vnc_write_u8(vs, vs->vd->auth);
22623e230dd2SCorentin Chary         vnc_read_when(vs, protocol_client_auth, 1);
22633e230dd2SCorentin Chary         vnc_flush(vs);
22643e230dd2SCorentin Chary     }
22653e230dd2SCorentin Chary 
22663e230dd2SCorentin Chary     return 0;
22673e230dd2SCorentin Chary }
22683e230dd2SCorentin Chary 
2269999342a0SCorentin Chary static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y)
2270999342a0SCorentin Chary {
2271999342a0SCorentin Chary     struct VncSurface *vs = &vd->guest;
2272999342a0SCorentin Chary 
2273999342a0SCorentin Chary     return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT];
2274999342a0SCorentin Chary }
2275999342a0SCorentin Chary 
2276*7d964c9dSCorentin Chary void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h)
2277*7d964c9dSCorentin Chary {
2278*7d964c9dSCorentin Chary     int i, j;
2279*7d964c9dSCorentin Chary 
2280*7d964c9dSCorentin Chary     w = (x + w) / VNC_STAT_RECT;
2281*7d964c9dSCorentin Chary     h = (y + h) / VNC_STAT_RECT;
2282*7d964c9dSCorentin Chary     x /= VNC_STAT_RECT;
2283*7d964c9dSCorentin Chary     y /= VNC_STAT_RECT;
2284*7d964c9dSCorentin Chary 
2285*7d964c9dSCorentin Chary     for (j = y; j <= y + h; j++) {
2286*7d964c9dSCorentin Chary         for (i = x; i <= x + w; i++) {
2287*7d964c9dSCorentin Chary             vs->lossy_rect[j][i] = 1;
2288*7d964c9dSCorentin Chary         }
2289*7d964c9dSCorentin Chary     }
2290*7d964c9dSCorentin Chary }
2291*7d964c9dSCorentin Chary 
2292*7d964c9dSCorentin Chary static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y)
2293*7d964c9dSCorentin Chary {
2294*7d964c9dSCorentin Chary     VncState *vs;
2295*7d964c9dSCorentin Chary     int sty = y / VNC_STAT_RECT;
2296*7d964c9dSCorentin Chary     int stx = x / VNC_STAT_RECT;
2297*7d964c9dSCorentin Chary     int has_dirty = 0;
2298*7d964c9dSCorentin Chary 
2299*7d964c9dSCorentin Chary     y = y / VNC_STAT_RECT * VNC_STAT_RECT;
2300*7d964c9dSCorentin Chary     x = x / VNC_STAT_RECT * VNC_STAT_RECT;
2301*7d964c9dSCorentin Chary 
2302*7d964c9dSCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
2303*7d964c9dSCorentin Chary         int j;
2304*7d964c9dSCorentin Chary 
2305*7d964c9dSCorentin Chary         /* kernel send buffers are full -> refresh later */
2306*7d964c9dSCorentin Chary         if (vs->output.offset) {
2307*7d964c9dSCorentin Chary             continue;
2308*7d964c9dSCorentin Chary         }
2309*7d964c9dSCorentin Chary 
2310*7d964c9dSCorentin Chary         if (!vs->lossy_rect[sty][stx]) {
2311*7d964c9dSCorentin Chary             continue;
2312*7d964c9dSCorentin Chary         }
2313*7d964c9dSCorentin Chary         vs->lossy_rect[sty][stx] = 0;
2314*7d964c9dSCorentin Chary         for (j = 0; j < VNC_STAT_RECT; ++j) {
2315*7d964c9dSCorentin Chary             vnc_set_bits(vs->dirty[y + j], x / 16, VNC_STAT_RECT / 16);
2316*7d964c9dSCorentin Chary         }
2317*7d964c9dSCorentin Chary         has_dirty++;
2318*7d964c9dSCorentin Chary     }
2319*7d964c9dSCorentin Chary     return has_dirty;
2320*7d964c9dSCorentin Chary }
2321*7d964c9dSCorentin Chary 
2322*7d964c9dSCorentin Chary static int vnc_update_stats(VncDisplay *vd,  struct timeval * tv)
2323999342a0SCorentin Chary {
2324999342a0SCorentin Chary     int x, y;
2325999342a0SCorentin Chary     struct timeval res;
2326*7d964c9dSCorentin Chary     int has_dirty = 0;
2327999342a0SCorentin Chary 
2328999342a0SCorentin Chary     for (y = 0; y < vd->guest.ds->height; y += VNC_STAT_RECT) {
2329999342a0SCorentin Chary         for (x = 0; x < vd->guest.ds->width; x += VNC_STAT_RECT) {
2330999342a0SCorentin Chary             VncRectStat *rect = vnc_stat_rect(vd, x, y);
2331999342a0SCorentin Chary 
2332999342a0SCorentin Chary             rect->updated = false;
2333999342a0SCorentin Chary         }
2334999342a0SCorentin Chary     }
2335999342a0SCorentin Chary 
2336999342a0SCorentin Chary     timersub(tv, &VNC_REFRESH_STATS, &res);
2337999342a0SCorentin Chary 
2338999342a0SCorentin Chary     if (timercmp(&vd->guest.last_freq_check, &res, >)) {
2339*7d964c9dSCorentin Chary         return has_dirty;
2340999342a0SCorentin Chary     }
2341999342a0SCorentin Chary     vd->guest.last_freq_check = *tv;
2342999342a0SCorentin Chary 
2343999342a0SCorentin Chary     for (y = 0; y < vd->guest.ds->height; y += VNC_STAT_RECT) {
2344999342a0SCorentin Chary         for (x = 0; x < vd->guest.ds->width; x += VNC_STAT_RECT) {
2345999342a0SCorentin Chary             VncRectStat *rect= vnc_stat_rect(vd, x, y);
2346999342a0SCorentin Chary             int count = ARRAY_SIZE(rect->times);
2347999342a0SCorentin Chary             struct timeval min, max;
2348999342a0SCorentin Chary 
2349999342a0SCorentin Chary             if (!timerisset(&rect->times[count - 1])) {
2350999342a0SCorentin Chary                 continue ;
2351999342a0SCorentin Chary             }
2352999342a0SCorentin Chary 
2353999342a0SCorentin Chary             max = rect->times[(rect->idx + count - 1) % count];
2354999342a0SCorentin Chary             timersub(tv, &max, &res);
2355999342a0SCorentin Chary 
2356999342a0SCorentin Chary             if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) {
2357999342a0SCorentin Chary                 rect->freq = 0;
2358*7d964c9dSCorentin Chary                 has_dirty += vnc_refresh_lossy_rect(vd, x, y);
2359999342a0SCorentin Chary                 memset(rect->times, 0, sizeof (rect->times));
2360999342a0SCorentin Chary                 continue ;
2361999342a0SCorentin Chary             }
2362999342a0SCorentin Chary 
2363999342a0SCorentin Chary             min = rect->times[rect->idx];
2364999342a0SCorentin Chary             max = rect->times[(rect->idx + count - 1) % count];
2365999342a0SCorentin Chary             timersub(&max, &min, &res);
2366999342a0SCorentin Chary 
2367999342a0SCorentin Chary             rect->freq = res.tv_sec + res.tv_usec / 1000000.;
2368999342a0SCorentin Chary             rect->freq /= count;
2369999342a0SCorentin Chary             rect->freq = 1. / rect->freq;
2370999342a0SCorentin Chary         }
2371999342a0SCorentin Chary     }
2372*7d964c9dSCorentin Chary     return has_dirty;
2373999342a0SCorentin Chary }
2374999342a0SCorentin Chary 
2375999342a0SCorentin Chary double vnc_update_freq(VncState *vs, int x, int y, int w, int h)
2376999342a0SCorentin Chary {
2377999342a0SCorentin Chary     int i, j;
2378999342a0SCorentin Chary     double total = 0;
2379999342a0SCorentin Chary     int num = 0;
2380999342a0SCorentin Chary 
2381999342a0SCorentin Chary     x =  (x / VNC_STAT_RECT) * VNC_STAT_RECT;
2382999342a0SCorentin Chary     y =  (y / VNC_STAT_RECT) * VNC_STAT_RECT;
2383999342a0SCorentin Chary 
2384999342a0SCorentin Chary     for (j = y; j <= y + h; j += VNC_STAT_RECT) {
2385999342a0SCorentin Chary         for (i = x; i <= x + w; i += VNC_STAT_RECT) {
2386999342a0SCorentin Chary             total += vnc_stat_rect(vs->vd, i, j)->freq;
2387999342a0SCorentin Chary             num++;
2388999342a0SCorentin Chary         }
2389999342a0SCorentin Chary     }
2390999342a0SCorentin Chary 
2391999342a0SCorentin Chary     if (num) {
2392999342a0SCorentin Chary         return total / num;
2393999342a0SCorentin Chary     } else {
2394999342a0SCorentin Chary         return 0;
2395999342a0SCorentin Chary     }
2396999342a0SCorentin Chary }
2397999342a0SCorentin Chary 
2398999342a0SCorentin Chary static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv)
2399999342a0SCorentin Chary {
2400999342a0SCorentin Chary     VncRectStat *rect;
2401999342a0SCorentin Chary 
2402999342a0SCorentin Chary     rect = vnc_stat_rect(vd, x, y);
2403999342a0SCorentin Chary     if (rect->updated) {
2404999342a0SCorentin Chary         return ;
2405999342a0SCorentin Chary     }
2406999342a0SCorentin Chary     rect->times[rect->idx] = *tv;
2407999342a0SCorentin Chary     rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times);
2408999342a0SCorentin Chary     rect->updated = true;
2409999342a0SCorentin Chary }
2410999342a0SCorentin Chary 
24113e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd)
24123e230dd2SCorentin Chary {
24133e230dd2SCorentin Chary     int y;
24143e230dd2SCorentin Chary     uint8_t *guest_row;
24153e230dd2SCorentin Chary     uint8_t *server_row;
24163e230dd2SCorentin Chary     int cmp_bytes;
24173e230dd2SCorentin Chary     uint32_t width_mask[VNC_DIRTY_WORDS];
24183e230dd2SCorentin Chary     VncState *vs;
24193e230dd2SCorentin Chary     int has_dirty = 0;
24203e230dd2SCorentin Chary 
2421999342a0SCorentin Chary     struct timeval tv;
2422999342a0SCorentin Chary 
2423999342a0SCorentin Chary     gettimeofday(&tv, NULL);
2424*7d964c9dSCorentin Chary     has_dirty = vnc_update_stats(vd, &tv);
2425999342a0SCorentin Chary 
24263e230dd2SCorentin Chary     /*
24273e230dd2SCorentin Chary      * Walk through the guest dirty map.
24283e230dd2SCorentin Chary      * Check and copy modified bits from guest to server surface.
24293e230dd2SCorentin Chary      * Update server dirty map.
24303e230dd2SCorentin Chary      */
24313e230dd2SCorentin Chary     vnc_set_bits(width_mask, (ds_get_width(vd->ds) / 16), VNC_DIRTY_WORDS);
24323e230dd2SCorentin Chary     cmp_bytes = 16 * ds_get_bytes_per_pixel(vd->ds);
24333e230dd2SCorentin Chary     guest_row  = vd->guest.ds->data;
24343e230dd2SCorentin Chary     server_row = vd->server->data;
24353e230dd2SCorentin Chary     for (y = 0; y < vd->guest.ds->height; y++) {
24363e230dd2SCorentin Chary         if (vnc_and_bits(vd->guest.dirty[y], width_mask, VNC_DIRTY_WORDS)) {
24373e230dd2SCorentin Chary             int x;
24383e230dd2SCorentin Chary             uint8_t *guest_ptr;
24393e230dd2SCorentin Chary             uint8_t *server_ptr;
24403e230dd2SCorentin Chary 
24413e230dd2SCorentin Chary             guest_ptr  = guest_row;
24423e230dd2SCorentin Chary             server_ptr = server_row;
24433e230dd2SCorentin Chary 
24443e230dd2SCorentin Chary             for (x = 0; x < vd->guest.ds->width;
24453e230dd2SCorentin Chary                     x += 16, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) {
24463e230dd2SCorentin Chary                 if (!vnc_get_bit(vd->guest.dirty[y], (x / 16)))
24473e230dd2SCorentin Chary                     continue;
24483e230dd2SCorentin Chary                 vnc_clear_bit(vd->guest.dirty[y], (x / 16));
24493e230dd2SCorentin Chary                 if (memcmp(server_ptr, guest_ptr, cmp_bytes) == 0)
24503e230dd2SCorentin Chary                     continue;
24513e230dd2SCorentin Chary                 memcpy(server_ptr, guest_ptr, cmp_bytes);
2452999342a0SCorentin Chary                 vnc_rect_updated(vd, x, y, &tv);
24533e230dd2SCorentin Chary                 QTAILQ_FOREACH(vs, &vd->clients, next) {
24543e230dd2SCorentin Chary                     vnc_set_bit(vs->dirty[y], (x / 16));
24553e230dd2SCorentin Chary                 }
24563e230dd2SCorentin Chary                 has_dirty++;
24573e230dd2SCorentin Chary             }
24583e230dd2SCorentin Chary         }
24593e230dd2SCorentin Chary         guest_row  += ds_get_linesize(vd->ds);
24603e230dd2SCorentin Chary         server_row += ds_get_linesize(vd->ds);
24613e230dd2SCorentin Chary     }
24623e230dd2SCorentin Chary     return has_dirty;
24633e230dd2SCorentin Chary }
24643e230dd2SCorentin Chary 
24653e230dd2SCorentin Chary static void vnc_refresh(void *opaque)
24663e230dd2SCorentin Chary {
24673e230dd2SCorentin Chary     VncDisplay *vd = opaque;
24683e230dd2SCorentin Chary     VncState *vs, *vn;
24693e230dd2SCorentin Chary     int has_dirty, rects = 0;
24703e230dd2SCorentin Chary 
24713e230dd2SCorentin Chary     vga_hw_update();
24723e230dd2SCorentin Chary 
2473bd023f95SCorentin Chary     if (vnc_trylock_display(vd)) {
2474bd023f95SCorentin Chary         vd->timer_interval = VNC_REFRESH_INTERVAL_BASE;
2475bd023f95SCorentin Chary         qemu_mod_timer(vd->timer, qemu_get_clock(rt_clock) +
2476bd023f95SCorentin Chary                        vd->timer_interval);
2477bd023f95SCorentin Chary         return;
2478bd023f95SCorentin Chary     }
2479bd023f95SCorentin Chary 
24803e230dd2SCorentin Chary     has_dirty = vnc_refresh_server_surface(vd);
2481bd023f95SCorentin Chary     vnc_unlock_display(vd);
24823e230dd2SCorentin Chary 
24833e230dd2SCorentin Chary     QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
24843e230dd2SCorentin Chary         rects += vnc_update_client(vs, has_dirty);
24853e230dd2SCorentin Chary         /* vs might be free()ed here */
24863e230dd2SCorentin Chary     }
2487bd023f95SCorentin Chary 
24883e230dd2SCorentin Chary     /* vd->timer could be NULL now if the last client disconnected,
24893e230dd2SCorentin Chary      * in this case don't update the timer */
24903e230dd2SCorentin Chary     if (vd->timer == NULL)
24913e230dd2SCorentin Chary         return;
24923e230dd2SCorentin Chary 
24933e230dd2SCorentin Chary     if (has_dirty && rects) {
24943e230dd2SCorentin Chary         vd->timer_interval /= 2;
24953e230dd2SCorentin Chary         if (vd->timer_interval < VNC_REFRESH_INTERVAL_BASE)
24963e230dd2SCorentin Chary             vd->timer_interval = VNC_REFRESH_INTERVAL_BASE;
24973e230dd2SCorentin Chary     } else {
24983e230dd2SCorentin Chary         vd->timer_interval += VNC_REFRESH_INTERVAL_INC;
24993e230dd2SCorentin Chary         if (vd->timer_interval > VNC_REFRESH_INTERVAL_MAX)
25003e230dd2SCorentin Chary             vd->timer_interval = VNC_REFRESH_INTERVAL_MAX;
25013e230dd2SCorentin Chary     }
25023e230dd2SCorentin Chary     qemu_mod_timer(vd->timer, qemu_get_clock(rt_clock) + vd->timer_interval);
25033e230dd2SCorentin Chary }
25043e230dd2SCorentin Chary 
25053e230dd2SCorentin Chary static void vnc_init_timer(VncDisplay *vd)
25063e230dd2SCorentin Chary {
25073e230dd2SCorentin Chary     vd->timer_interval = VNC_REFRESH_INTERVAL_BASE;
25083e230dd2SCorentin Chary     if (vd->timer == NULL && !QTAILQ_EMPTY(&vd->clients)) {
25093e230dd2SCorentin Chary         vd->timer = qemu_new_timer(rt_clock, vnc_refresh, vd);
25103e230dd2SCorentin Chary         vnc_refresh(vd);
25113e230dd2SCorentin Chary     }
25123e230dd2SCorentin Chary }
25133e230dd2SCorentin Chary 
25143e230dd2SCorentin Chary static void vnc_remove_timer(VncDisplay *vd)
25153e230dd2SCorentin Chary {
25163e230dd2SCorentin Chary     if (vd->timer != NULL && QTAILQ_EMPTY(&vd->clients)) {
25173e230dd2SCorentin Chary         qemu_del_timer(vd->timer);
25183e230dd2SCorentin Chary         qemu_free_timer(vd->timer);
25193e230dd2SCorentin Chary         vd->timer = NULL;
25203e230dd2SCorentin Chary     }
25213e230dd2SCorentin Chary }
25223e230dd2SCorentin Chary 
25233e230dd2SCorentin Chary static void vnc_connect(VncDisplay *vd, int csock)
25243e230dd2SCorentin Chary {
25253e230dd2SCorentin Chary     VncState *vs = qemu_mallocz(sizeof(VncState));
2526*7d964c9dSCorentin Chary     int i;
2527*7d964c9dSCorentin Chary 
25283e230dd2SCorentin Chary     vs->csock = csock;
2529*7d964c9dSCorentin Chary     vs->lossy_rect = qemu_mallocz(VNC_STAT_ROWS * sizeof (*vs->lossy_rect));
2530*7d964c9dSCorentin Chary     for (i = 0; i < VNC_STAT_ROWS; ++i) {
2531*7d964c9dSCorentin Chary         vs->lossy_rect[i] = qemu_mallocz(VNC_STAT_COLS * sizeof (uint8_t));
2532*7d964c9dSCorentin Chary     }
25333e230dd2SCorentin Chary 
25343e230dd2SCorentin Chary     VNC_DEBUG("New client on socket %d\n", csock);
25353e230dd2SCorentin Chary     dcl->idle = 0;
25363e230dd2SCorentin Chary     socket_set_nonblock(vs->csock);
25373e230dd2SCorentin Chary     qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
25383e230dd2SCorentin Chary 
25393e230dd2SCorentin Chary     vnc_client_cache_addr(vs);
25403e230dd2SCorentin Chary     vnc_qmp_event(vs, QEVENT_VNC_CONNECTED);
25413e230dd2SCorentin Chary 
25423e230dd2SCorentin Chary     vs->vd = vd;
25433e230dd2SCorentin Chary     vs->ds = vd->ds;
25443e230dd2SCorentin Chary     vs->last_x = -1;
25453e230dd2SCorentin Chary     vs->last_y = -1;
25463e230dd2SCorentin Chary 
25473e230dd2SCorentin Chary     vs->as.freq = 44100;
25483e230dd2SCorentin Chary     vs->as.nchannels = 2;
25493e230dd2SCorentin Chary     vs->as.fmt = AUD_FMT_S16;
25503e230dd2SCorentin Chary     vs->as.endianness = 0;
25513e230dd2SCorentin Chary 
2552bd023f95SCorentin Chary #ifdef CONFIG_VNC_THREAD
2553bd023f95SCorentin Chary     qemu_mutex_init(&vs->output_mutex);
2554bd023f95SCorentin Chary #endif
2555bd023f95SCorentin Chary 
25563e230dd2SCorentin Chary     QTAILQ_INSERT_HEAD(&vd->clients, vs, next);
25573e230dd2SCorentin Chary 
25583e230dd2SCorentin Chary     vga_hw_update();
25593e230dd2SCorentin Chary 
25603e230dd2SCorentin Chary     vnc_write(vs, "RFB 003.008\n", 12);
25613e230dd2SCorentin Chary     vnc_flush(vs);
25623e230dd2SCorentin Chary     vnc_read_when(vs, protocol_version, 12);
25633e230dd2SCorentin Chary     reset_keys(vs);
25643e230dd2SCorentin Chary     if (vs->vd->lock_key_sync)
25653e230dd2SCorentin Chary         vs->led = qemu_add_led_event_handler(kbd_leds, vs);
25663e230dd2SCorentin Chary 
25673e230dd2SCorentin Chary     vs->mouse_mode_notifier.notify = check_pointer_type_change;
25683e230dd2SCorentin Chary     qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
25693e230dd2SCorentin Chary 
25703e230dd2SCorentin Chary     vnc_init_timer(vd);
25713e230dd2SCorentin Chary 
25723e230dd2SCorentin Chary     /* vs might be free()ed here */
25733e230dd2SCorentin Chary }
25743e230dd2SCorentin Chary 
25753e230dd2SCorentin Chary static void vnc_listen_read(void *opaque)
25763e230dd2SCorentin Chary {
25773e230dd2SCorentin Chary     VncDisplay *vs = opaque;
25783e230dd2SCorentin Chary     struct sockaddr_in addr;
25793e230dd2SCorentin Chary     socklen_t addrlen = sizeof(addr);
25803e230dd2SCorentin Chary 
25813e230dd2SCorentin Chary     /* Catch-up */
25823e230dd2SCorentin Chary     vga_hw_update();
25833e230dd2SCorentin Chary 
25843e230dd2SCorentin Chary     int csock = qemu_accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
25853e230dd2SCorentin Chary     if (csock != -1) {
25863e230dd2SCorentin Chary         vnc_connect(vs, csock);
25873e230dd2SCorentin Chary     }
25883e230dd2SCorentin Chary }
25893e230dd2SCorentin Chary 
25903e230dd2SCorentin Chary void vnc_display_init(DisplayState *ds)
25913e230dd2SCorentin Chary {
25923e230dd2SCorentin Chary     VncDisplay *vs = qemu_mallocz(sizeof(*vs));
25933e230dd2SCorentin Chary 
25943e230dd2SCorentin Chary     dcl = qemu_mallocz(sizeof(DisplayChangeListener));
25953e230dd2SCorentin Chary 
25963e230dd2SCorentin Chary     ds->opaque = vs;
25973e230dd2SCorentin Chary     dcl->idle = 1;
25983e230dd2SCorentin Chary     vnc_display = vs;
25993e230dd2SCorentin Chary 
26003e230dd2SCorentin Chary     vs->lsock = -1;
26013e230dd2SCorentin Chary 
26023e230dd2SCorentin Chary     vs->ds = ds;
26033e230dd2SCorentin Chary     QTAILQ_INIT(&vs->clients);
26043c9405a0SGerd Hoffmann     vs->expires = TIME_MAX;
26053e230dd2SCorentin Chary 
26063e230dd2SCorentin Chary     if (keyboard_layout)
26073e230dd2SCorentin Chary         vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
26083e230dd2SCorentin Chary     else
26093e230dd2SCorentin Chary         vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us");
26103e230dd2SCorentin Chary 
26113e230dd2SCorentin Chary     if (!vs->kbd_layout)
26123e230dd2SCorentin Chary         exit(1);
26133e230dd2SCorentin Chary 
2614bd023f95SCorentin Chary #ifdef CONFIG_VNC_THREAD
2615bd023f95SCorentin Chary     qemu_mutex_init(&vs->mutex);
2616bd023f95SCorentin Chary     vnc_start_worker_thread();
2617bd023f95SCorentin Chary #endif
2618bd023f95SCorentin Chary 
26193e230dd2SCorentin Chary     dcl->dpy_copy = vnc_dpy_copy;
26203e230dd2SCorentin Chary     dcl->dpy_update = vnc_dpy_update;
26213e230dd2SCorentin Chary     dcl->dpy_resize = vnc_dpy_resize;
26223e230dd2SCorentin Chary     dcl->dpy_setdata = vnc_dpy_setdata;
26233e230dd2SCorentin Chary     register_displaychangelistener(ds, dcl);
26243e230dd2SCorentin Chary     ds->mouse_set = vnc_mouse_set;
26253e230dd2SCorentin Chary     ds->cursor_define = vnc_dpy_cursor_define;
26263e230dd2SCorentin Chary }
26273e230dd2SCorentin Chary 
26283e230dd2SCorentin Chary 
26293e230dd2SCorentin Chary void vnc_display_close(DisplayState *ds)
26303e230dd2SCorentin Chary {
26313e230dd2SCorentin Chary     VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
26323e230dd2SCorentin Chary 
26333e230dd2SCorentin Chary     if (!vs)
26343e230dd2SCorentin Chary         return;
26353e230dd2SCorentin Chary     if (vs->display) {
26363e230dd2SCorentin Chary         qemu_free(vs->display);
26373e230dd2SCorentin Chary         vs->display = NULL;
26383e230dd2SCorentin Chary     }
26393e230dd2SCorentin Chary     if (vs->lsock != -1) {
26403e230dd2SCorentin Chary         qemu_set_fd_handler2(vs->lsock, NULL, NULL, NULL, NULL);
26413e230dd2SCorentin Chary         close(vs->lsock);
26423e230dd2SCorentin Chary         vs->lsock = -1;
26433e230dd2SCorentin Chary     }
26443e230dd2SCorentin Chary     vs->auth = VNC_AUTH_INVALID;
26453e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
26463e230dd2SCorentin Chary     vs->subauth = VNC_AUTH_INVALID;
26473e230dd2SCorentin Chary     vs->tls.x509verify = 0;
26483e230dd2SCorentin Chary #endif
26493e230dd2SCorentin Chary }
26503e230dd2SCorentin Chary 
26511cd20f8bSAnthony Liguori int vnc_display_disable_login(DisplayState *ds)
26523e230dd2SCorentin Chary {
26533e230dd2SCorentin Chary     VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
26543e230dd2SCorentin Chary 
26553e230dd2SCorentin Chary     if (!vs) {
26563e230dd2SCorentin Chary         return -1;
26573e230dd2SCorentin Chary     }
26583e230dd2SCorentin Chary 
26593e230dd2SCorentin Chary     if (vs->password) {
26603e230dd2SCorentin Chary         qemu_free(vs->password);
26611cd20f8bSAnthony Liguori     }
26621cd20f8bSAnthony Liguori 
26631cd20f8bSAnthony Liguori     vs->password = NULL;
26641cd20f8bSAnthony Liguori     vs->auth = VNC_AUTH_VNC;
26651cd20f8bSAnthony Liguori 
26661cd20f8bSAnthony Liguori     return 0;
26671cd20f8bSAnthony Liguori }
26681cd20f8bSAnthony Liguori 
26691cd20f8bSAnthony Liguori int vnc_display_password(DisplayState *ds, const char *password)
26701cd20f8bSAnthony Liguori {
26711cd20f8bSAnthony Liguori     VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
26721cd20f8bSAnthony Liguori 
26731cd20f8bSAnthony Liguori     if (!vs) {
26741cd20f8bSAnthony Liguori         return -1;
26751cd20f8bSAnthony Liguori     }
26761cd20f8bSAnthony Liguori 
26771cd20f8bSAnthony Liguori     if (!password) {
26781cd20f8bSAnthony Liguori         /* This is not the intention of this interface but err on the side
26791cd20f8bSAnthony Liguori            of being safe */
26801cd20f8bSAnthony Liguori         return vnc_display_disable_login(ds);
26811cd20f8bSAnthony Liguori     }
26821cd20f8bSAnthony Liguori 
26831cd20f8bSAnthony Liguori     if (vs->password) {
26841cd20f8bSAnthony Liguori         qemu_free(vs->password);
26853e230dd2SCorentin Chary         vs->password = NULL;
26863e230dd2SCorentin Chary     }
26871cd20f8bSAnthony Liguori     vs->password = qemu_strdup(password);
26883e230dd2SCorentin Chary     vs->auth = VNC_AUTH_VNC;
26893e230dd2SCorentin Chary 
26903e230dd2SCorentin Chary     return 0;
26913e230dd2SCorentin Chary }
26923e230dd2SCorentin Chary 
26933c9405a0SGerd Hoffmann int vnc_display_pw_expire(DisplayState *ds, time_t expires)
26943c9405a0SGerd Hoffmann {
26953c9405a0SGerd Hoffmann     VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
26963c9405a0SGerd Hoffmann 
26973c9405a0SGerd Hoffmann     vs->expires = expires;
26983c9405a0SGerd Hoffmann     return 0;
26993c9405a0SGerd Hoffmann }
27003c9405a0SGerd Hoffmann 
27013e230dd2SCorentin Chary char *vnc_display_local_addr(DisplayState *ds)
27023e230dd2SCorentin Chary {
27033e230dd2SCorentin Chary     VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
27043e230dd2SCorentin Chary 
27053e230dd2SCorentin Chary     return vnc_socket_local_addr("%s:%s", vs->lsock);
27063e230dd2SCorentin Chary }
27073e230dd2SCorentin Chary 
27083e230dd2SCorentin Chary int vnc_display_open(DisplayState *ds, const char *display)
27093e230dd2SCorentin Chary {
27103e230dd2SCorentin Chary     VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
27113e230dd2SCorentin Chary     const char *options;
27123e230dd2SCorentin Chary     int password = 0;
27133e230dd2SCorentin Chary     int reverse = 0;
27143e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
27153e230dd2SCorentin Chary     int tls = 0, x509 = 0;
27163e230dd2SCorentin Chary #endif
27173e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
27183e230dd2SCorentin Chary     int sasl = 0;
27193e230dd2SCorentin Chary     int saslErr;
27203e230dd2SCorentin Chary #endif
27212ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
27223e230dd2SCorentin Chary     int acl = 0;
27232ded6ad7SBlue Swirl #endif
27243e230dd2SCorentin Chary     int lock_key_sync = 1;
27253e230dd2SCorentin Chary 
27263e230dd2SCorentin Chary     if (!vnc_display)
27273e230dd2SCorentin Chary         return -1;
27283e230dd2SCorentin Chary     vnc_display_close(ds);
27293e230dd2SCorentin Chary     if (strcmp(display, "none") == 0)
27303e230dd2SCorentin Chary         return 0;
27313e230dd2SCorentin Chary 
27323e230dd2SCorentin Chary     if (!(vs->display = strdup(display)))
27333e230dd2SCorentin Chary         return -1;
27343e230dd2SCorentin Chary 
27353e230dd2SCorentin Chary     options = display;
27363e230dd2SCorentin Chary     while ((options = strchr(options, ','))) {
27373e230dd2SCorentin Chary         options++;
27383e230dd2SCorentin Chary         if (strncmp(options, "password", 8) == 0) {
27393e230dd2SCorentin Chary             password = 1; /* Require password auth */
27403e230dd2SCorentin Chary         } else if (strncmp(options, "reverse", 7) == 0) {
27413e230dd2SCorentin Chary             reverse = 1;
27423e230dd2SCorentin Chary         } else if (strncmp(options, "no-lock-key-sync", 9) == 0) {
27433e230dd2SCorentin Chary             lock_key_sync = 0;
27443e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
27453e230dd2SCorentin Chary         } else if (strncmp(options, "sasl", 4) == 0) {
27463e230dd2SCorentin Chary             sasl = 1; /* Require SASL auth */
27473e230dd2SCorentin Chary #endif
27483e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
27493e230dd2SCorentin Chary         } else if (strncmp(options, "tls", 3) == 0) {
27503e230dd2SCorentin Chary             tls = 1; /* Require TLS */
27513e230dd2SCorentin Chary         } else if (strncmp(options, "x509", 4) == 0) {
27523e230dd2SCorentin Chary             char *start, *end;
27533e230dd2SCorentin Chary             x509 = 1; /* Require x509 certificates */
27543e230dd2SCorentin Chary             if (strncmp(options, "x509verify", 10) == 0)
27553e230dd2SCorentin Chary                 vs->tls.x509verify = 1; /* ...and verify client certs */
27563e230dd2SCorentin Chary 
27573e230dd2SCorentin Chary             /* Now check for 'x509=/some/path' postfix
27583e230dd2SCorentin Chary              * and use that to setup x509 certificate/key paths */
27593e230dd2SCorentin Chary             start = strchr(options, '=');
27603e230dd2SCorentin Chary             end = strchr(options, ',');
27613e230dd2SCorentin Chary             if (start && (!end || (start < end))) {
27623e230dd2SCorentin Chary                 int len = end ? end-(start+1) : strlen(start+1);
27633e230dd2SCorentin Chary                 char *path = qemu_strndup(start + 1, len);
27643e230dd2SCorentin Chary 
27653e230dd2SCorentin Chary                 VNC_DEBUG("Trying certificate path '%s'\n", path);
27663e230dd2SCorentin Chary                 if (vnc_tls_set_x509_creds_dir(vs, path) < 0) {
27673e230dd2SCorentin Chary                     fprintf(stderr, "Failed to find x509 certificates/keys in %s\n", path);
27683e230dd2SCorentin Chary                     qemu_free(path);
27693e230dd2SCorentin Chary                     qemu_free(vs->display);
27703e230dd2SCorentin Chary                     vs->display = NULL;
27713e230dd2SCorentin Chary                     return -1;
27723e230dd2SCorentin Chary                 }
27733e230dd2SCorentin Chary                 qemu_free(path);
27743e230dd2SCorentin Chary             } else {
27753e230dd2SCorentin Chary                 fprintf(stderr, "No certificate path provided\n");
27763e230dd2SCorentin Chary                 qemu_free(vs->display);
27773e230dd2SCorentin Chary                 vs->display = NULL;
27783e230dd2SCorentin Chary                 return -1;
27793e230dd2SCorentin Chary             }
27803e230dd2SCorentin Chary #endif
27812ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
27823e230dd2SCorentin Chary         } else if (strncmp(options, "acl", 3) == 0) {
27833e230dd2SCorentin Chary             acl = 1;
27842ded6ad7SBlue Swirl #endif
27853e230dd2SCorentin Chary         } else if (strncmp(options, "lossy", 5) == 0) {
27863e230dd2SCorentin Chary             vs->lossy = true;
27873e230dd2SCorentin Chary         }
27883e230dd2SCorentin Chary     }
27893e230dd2SCorentin Chary 
27903e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
27913e230dd2SCorentin Chary     if (acl && x509 && vs->tls.x509verify) {
27923e230dd2SCorentin Chary         if (!(vs->tls.acl = qemu_acl_init("vnc.x509dname"))) {
27933e230dd2SCorentin Chary             fprintf(stderr, "Failed to create x509 dname ACL\n");
27943e230dd2SCorentin Chary             exit(1);
27953e230dd2SCorentin Chary         }
27963e230dd2SCorentin Chary     }
27973e230dd2SCorentin Chary #endif
27983e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
27993e230dd2SCorentin Chary     if (acl && sasl) {
28003e230dd2SCorentin Chary         if (!(vs->sasl.acl = qemu_acl_init("vnc.username"))) {
28013e230dd2SCorentin Chary             fprintf(stderr, "Failed to create username ACL\n");
28023e230dd2SCorentin Chary             exit(1);
28033e230dd2SCorentin Chary         }
28043e230dd2SCorentin Chary     }
28053e230dd2SCorentin Chary #endif
28063e230dd2SCorentin Chary 
28073e230dd2SCorentin Chary     /*
28083e230dd2SCorentin Chary      * Combinations we support here:
28093e230dd2SCorentin Chary      *
28103e230dd2SCorentin Chary      *  - no-auth                (clear text, no auth)
28113e230dd2SCorentin Chary      *  - password               (clear text, weak auth)
28123e230dd2SCorentin Chary      *  - sasl                   (encrypt, good auth *IF* using Kerberos via GSSAPI)
28133e230dd2SCorentin Chary      *  - tls                    (encrypt, weak anonymous creds, no auth)
28143e230dd2SCorentin Chary      *  - tls + password         (encrypt, weak anonymous creds, weak auth)
28153e230dd2SCorentin Chary      *  - tls + sasl             (encrypt, weak anonymous creds, good auth)
28163e230dd2SCorentin Chary      *  - tls + x509             (encrypt, good x509 creds, no auth)
28173e230dd2SCorentin Chary      *  - tls + x509 + password  (encrypt, good x509 creds, weak auth)
28183e230dd2SCorentin Chary      *  - tls + x509 + sasl      (encrypt, good x509 creds, good auth)
28193e230dd2SCorentin Chary      *
28203e230dd2SCorentin Chary      * NB1. TLS is a stackable auth scheme.
28213e230dd2SCorentin Chary      * NB2. the x509 schemes have option to validate a client cert dname
28223e230dd2SCorentin Chary      */
28233e230dd2SCorentin Chary     if (password) {
28243e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
28253e230dd2SCorentin Chary         if (tls) {
28263e230dd2SCorentin Chary             vs->auth = VNC_AUTH_VENCRYPT;
28273e230dd2SCorentin Chary             if (x509) {
28283e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
28293e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_X509VNC;
28303e230dd2SCorentin Chary             } else {
28313e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
28323e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC;
28333e230dd2SCorentin Chary             }
28343e230dd2SCorentin Chary         } else {
28353e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
28363e230dd2SCorentin Chary             VNC_DEBUG("Initializing VNC server with password auth\n");
28373e230dd2SCorentin Chary             vs->auth = VNC_AUTH_VNC;
28383e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
28393e230dd2SCorentin Chary             vs->subauth = VNC_AUTH_INVALID;
28403e230dd2SCorentin Chary         }
28413e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
28423e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
28433e230dd2SCorentin Chary     } else if (sasl) {
28443e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
28453e230dd2SCorentin Chary         if (tls) {
28463e230dd2SCorentin Chary             vs->auth = VNC_AUTH_VENCRYPT;
28473e230dd2SCorentin Chary             if (x509) {
28483e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
28493e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_X509SASL;
28503e230dd2SCorentin Chary             } else {
28513e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
28523e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL;
28533e230dd2SCorentin Chary             }
28543e230dd2SCorentin Chary         } else {
28553e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
28563e230dd2SCorentin Chary             VNC_DEBUG("Initializing VNC server with SASL auth\n");
28573e230dd2SCorentin Chary             vs->auth = VNC_AUTH_SASL;
28583e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
28593e230dd2SCorentin Chary             vs->subauth = VNC_AUTH_INVALID;
28603e230dd2SCorentin Chary         }
28613e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
28623e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
28633e230dd2SCorentin Chary     } else {
28643e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
28653e230dd2SCorentin Chary         if (tls) {
28663e230dd2SCorentin Chary             vs->auth = VNC_AUTH_VENCRYPT;
28673e230dd2SCorentin Chary             if (x509) {
28683e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
28693e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_X509NONE;
28703e230dd2SCorentin Chary             } else {
28713e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
28723e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE;
28733e230dd2SCorentin Chary             }
28743e230dd2SCorentin Chary         } else {
28753e230dd2SCorentin Chary #endif
28763e230dd2SCorentin Chary             VNC_DEBUG("Initializing VNC server with no auth\n");
28773e230dd2SCorentin Chary             vs->auth = VNC_AUTH_NONE;
28783e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
28793e230dd2SCorentin Chary             vs->subauth = VNC_AUTH_INVALID;
28803e230dd2SCorentin Chary         }
28813e230dd2SCorentin Chary #endif
28823e230dd2SCorentin Chary     }
28833e230dd2SCorentin Chary 
28843e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
28853e230dd2SCorentin Chary     if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) {
28863e230dd2SCorentin Chary         fprintf(stderr, "Failed to initialize SASL auth %s",
28873e230dd2SCorentin Chary                 sasl_errstring(saslErr, NULL, NULL));
28883e230dd2SCorentin Chary         free(vs->display);
28893e230dd2SCorentin Chary         vs->display = NULL;
28903e230dd2SCorentin Chary         return -1;
28913e230dd2SCorentin Chary     }
28923e230dd2SCorentin Chary #endif
28933e230dd2SCorentin Chary     vs->lock_key_sync = lock_key_sync;
28943e230dd2SCorentin Chary 
28953e230dd2SCorentin Chary     if (reverse) {
28963e230dd2SCorentin Chary         /* connect to viewer */
28973e230dd2SCorentin Chary         if (strncmp(display, "unix:", 5) == 0)
28983e230dd2SCorentin Chary             vs->lsock = unix_connect(display+5);
28993e230dd2SCorentin Chary         else
29003e230dd2SCorentin Chary             vs->lsock = inet_connect(display, SOCK_STREAM);
29013e230dd2SCorentin Chary         if (-1 == vs->lsock) {
29023e230dd2SCorentin Chary             free(vs->display);
29033e230dd2SCorentin Chary             vs->display = NULL;
29043e230dd2SCorentin Chary             return -1;
29053e230dd2SCorentin Chary         } else {
29063e230dd2SCorentin Chary             int csock = vs->lsock;
29073e230dd2SCorentin Chary             vs->lsock = -1;
29083e230dd2SCorentin Chary             vnc_connect(vs, csock);
29093e230dd2SCorentin Chary         }
29103e230dd2SCorentin Chary         return 0;
29113e230dd2SCorentin Chary 
29123e230dd2SCorentin Chary     } else {
29133e230dd2SCorentin Chary         /* listen for connects */
29143e230dd2SCorentin Chary         char *dpy;
29153e230dd2SCorentin Chary         dpy = qemu_malloc(256);
29163e230dd2SCorentin Chary         if (strncmp(display, "unix:", 5) == 0) {
29173e230dd2SCorentin Chary             pstrcpy(dpy, 256, "unix:");
29183e230dd2SCorentin Chary             vs->lsock = unix_listen(display+5, dpy+5, 256-5);
29193e230dd2SCorentin Chary         } else {
29203e230dd2SCorentin Chary             vs->lsock = inet_listen(display, dpy, 256, SOCK_STREAM, 5900);
29213e230dd2SCorentin Chary         }
29223e230dd2SCorentin Chary         if (-1 == vs->lsock) {
29233e230dd2SCorentin Chary             free(dpy);
29243e230dd2SCorentin Chary             return -1;
29253e230dd2SCorentin Chary         } else {
29263e230dd2SCorentin Chary             free(vs->display);
29273e230dd2SCorentin Chary             vs->display = dpy;
29283e230dd2SCorentin Chary         }
29293e230dd2SCorentin Chary     }
29303e230dd2SCorentin Chary     return qemu_set_fd_handler2(vs->lsock, NULL, vnc_listen_read, NULL, vs);
29313e230dd2SCorentin Chary }
2932