xref: /openbmc/qemu/ui/vnc.c (revision 6c71a539c3cb3927fdea230c3ddb9e273142570f)
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 static VncDisplay *vnc_display; /* needed for info vnc */
453e230dd2SCorentin Chary static DisplayChangeListener *dcl;
463e230dd2SCorentin Chary 
473e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs);
483e230dd2SCorentin Chary 
493e230dd2SCorentin Chary static char *addr_to_string(const char *format,
503e230dd2SCorentin Chary                             struct sockaddr_storage *sa,
513e230dd2SCorentin Chary                             socklen_t salen) {
523e230dd2SCorentin Chary     char *addr;
533e230dd2SCorentin Chary     char host[NI_MAXHOST];
543e230dd2SCorentin Chary     char serv[NI_MAXSERV];
553e230dd2SCorentin Chary     int err;
563e230dd2SCorentin Chary     size_t addrlen;
573e230dd2SCorentin Chary 
583e230dd2SCorentin Chary     if ((err = getnameinfo((struct sockaddr *)sa, salen,
593e230dd2SCorentin Chary                            host, sizeof(host),
603e230dd2SCorentin Chary                            serv, sizeof(serv),
613e230dd2SCorentin Chary                            NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
623e230dd2SCorentin Chary         VNC_DEBUG("Cannot resolve address %d: %s\n",
633e230dd2SCorentin Chary                   err, gai_strerror(err));
643e230dd2SCorentin Chary         return NULL;
653e230dd2SCorentin Chary     }
663e230dd2SCorentin Chary 
673e230dd2SCorentin Chary     /* Enough for the existing format + the 2 vars we're
683e230dd2SCorentin Chary      * substituting in. */
693e230dd2SCorentin Chary     addrlen = strlen(format) + strlen(host) + strlen(serv);
703e230dd2SCorentin Chary     addr = qemu_malloc(addrlen + 1);
713e230dd2SCorentin Chary     snprintf(addr, addrlen, format, host, serv);
723e230dd2SCorentin Chary     addr[addrlen] = '\0';
733e230dd2SCorentin Chary 
743e230dd2SCorentin Chary     return addr;
753e230dd2SCorentin Chary }
763e230dd2SCorentin Chary 
773e230dd2SCorentin Chary 
783e230dd2SCorentin Chary char *vnc_socket_local_addr(const char *format, int fd) {
793e230dd2SCorentin Chary     struct sockaddr_storage sa;
803e230dd2SCorentin Chary     socklen_t salen;
813e230dd2SCorentin Chary 
823e230dd2SCorentin Chary     salen = sizeof(sa);
833e230dd2SCorentin Chary     if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0)
843e230dd2SCorentin Chary         return NULL;
853e230dd2SCorentin Chary 
863e230dd2SCorentin Chary     return addr_to_string(format, &sa, salen);
873e230dd2SCorentin Chary }
883e230dd2SCorentin Chary 
893e230dd2SCorentin Chary char *vnc_socket_remote_addr(const char *format, int fd) {
903e230dd2SCorentin Chary     struct sockaddr_storage sa;
913e230dd2SCorentin Chary     socklen_t salen;
923e230dd2SCorentin Chary 
933e230dd2SCorentin Chary     salen = sizeof(sa);
943e230dd2SCorentin Chary     if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0)
953e230dd2SCorentin Chary         return NULL;
963e230dd2SCorentin Chary 
973e230dd2SCorentin Chary     return addr_to_string(format, &sa, salen);
983e230dd2SCorentin Chary }
993e230dd2SCorentin Chary 
1003e230dd2SCorentin Chary static int put_addr_qdict(QDict *qdict, struct sockaddr_storage *sa,
1013e230dd2SCorentin Chary                           socklen_t salen)
1023e230dd2SCorentin Chary {
1033e230dd2SCorentin Chary     char host[NI_MAXHOST];
1043e230dd2SCorentin Chary     char serv[NI_MAXSERV];
1053e230dd2SCorentin Chary     int err;
1063e230dd2SCorentin Chary 
1073e230dd2SCorentin Chary     if ((err = getnameinfo((struct sockaddr *)sa, salen,
1083e230dd2SCorentin Chary                            host, sizeof(host),
1093e230dd2SCorentin Chary                            serv, sizeof(serv),
1103e230dd2SCorentin Chary                            NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
1113e230dd2SCorentin Chary         VNC_DEBUG("Cannot resolve address %d: %s\n",
1123e230dd2SCorentin Chary                   err, gai_strerror(err));
1133e230dd2SCorentin Chary         return -1;
1143e230dd2SCorentin Chary     }
1153e230dd2SCorentin Chary 
1163e230dd2SCorentin Chary     qdict_put(qdict, "host", qstring_from_str(host));
1173e230dd2SCorentin Chary     qdict_put(qdict, "service", qstring_from_str(serv));
1183e230dd2SCorentin Chary     qdict_put(qdict, "family",qstring_from_str(inet_strfamily(sa->ss_family)));
1193e230dd2SCorentin Chary 
1203e230dd2SCorentin Chary     return 0;
1213e230dd2SCorentin Chary }
1223e230dd2SCorentin Chary 
1233e230dd2SCorentin Chary static int vnc_server_addr_put(QDict *qdict, int fd)
1243e230dd2SCorentin Chary {
1253e230dd2SCorentin Chary     struct sockaddr_storage sa;
1263e230dd2SCorentin Chary     socklen_t salen;
1273e230dd2SCorentin Chary 
1283e230dd2SCorentin Chary     salen = sizeof(sa);
1293e230dd2SCorentin Chary     if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) {
1303e230dd2SCorentin Chary         return -1;
1313e230dd2SCorentin Chary     }
1323e230dd2SCorentin Chary 
1333e230dd2SCorentin Chary     return put_addr_qdict(qdict, &sa, salen);
1343e230dd2SCorentin Chary }
1353e230dd2SCorentin Chary 
1363e230dd2SCorentin Chary static int vnc_qdict_remote_addr(QDict *qdict, int fd)
1373e230dd2SCorentin Chary {
1383e230dd2SCorentin Chary     struct sockaddr_storage sa;
1393e230dd2SCorentin Chary     socklen_t salen;
1403e230dd2SCorentin Chary 
1413e230dd2SCorentin Chary     salen = sizeof(sa);
1423e230dd2SCorentin Chary     if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) {
1433e230dd2SCorentin Chary         return -1;
1443e230dd2SCorentin Chary     }
1453e230dd2SCorentin Chary 
1463e230dd2SCorentin Chary     return put_addr_qdict(qdict, &sa, salen);
1473e230dd2SCorentin Chary }
1483e230dd2SCorentin Chary 
1493e230dd2SCorentin Chary static const char *vnc_auth_name(VncDisplay *vd) {
1503e230dd2SCorentin Chary     switch (vd->auth) {
1513e230dd2SCorentin Chary     case VNC_AUTH_INVALID:
1523e230dd2SCorentin Chary         return "invalid";
1533e230dd2SCorentin Chary     case VNC_AUTH_NONE:
1543e230dd2SCorentin Chary         return "none";
1553e230dd2SCorentin Chary     case VNC_AUTH_VNC:
1563e230dd2SCorentin Chary         return "vnc";
1573e230dd2SCorentin Chary     case VNC_AUTH_RA2:
1583e230dd2SCorentin Chary         return "ra2";
1593e230dd2SCorentin Chary     case VNC_AUTH_RA2NE:
1603e230dd2SCorentin Chary         return "ra2ne";
1613e230dd2SCorentin Chary     case VNC_AUTH_TIGHT:
1623e230dd2SCorentin Chary         return "tight";
1633e230dd2SCorentin Chary     case VNC_AUTH_ULTRA:
1643e230dd2SCorentin Chary         return "ultra";
1653e230dd2SCorentin Chary     case VNC_AUTH_TLS:
1663e230dd2SCorentin Chary         return "tls";
1673e230dd2SCorentin Chary     case VNC_AUTH_VENCRYPT:
1683e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
1693e230dd2SCorentin Chary         switch (vd->subauth) {
1703e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_PLAIN:
1713e230dd2SCorentin Chary             return "vencrypt+plain";
1723e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSNONE:
1733e230dd2SCorentin Chary             return "vencrypt+tls+none";
1743e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSVNC:
1753e230dd2SCorentin Chary             return "vencrypt+tls+vnc";
1763e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSPLAIN:
1773e230dd2SCorentin Chary             return "vencrypt+tls+plain";
1783e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509NONE:
1793e230dd2SCorentin Chary             return "vencrypt+x509+none";
1803e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509VNC:
1813e230dd2SCorentin Chary             return "vencrypt+x509+vnc";
1823e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509PLAIN:
1833e230dd2SCorentin Chary             return "vencrypt+x509+plain";
1843e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSSASL:
1853e230dd2SCorentin Chary             return "vencrypt+tls+sasl";
1863e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509SASL:
1873e230dd2SCorentin Chary             return "vencrypt+x509+sasl";
1883e230dd2SCorentin Chary         default:
1893e230dd2SCorentin Chary             return "vencrypt";
1903e230dd2SCorentin Chary         }
1913e230dd2SCorentin Chary #else
1923e230dd2SCorentin Chary         return "vencrypt";
1933e230dd2SCorentin Chary #endif
1943e230dd2SCorentin Chary     case VNC_AUTH_SASL:
1953e230dd2SCorentin Chary         return "sasl";
1963e230dd2SCorentin Chary     }
1973e230dd2SCorentin Chary     return "unknown";
1983e230dd2SCorentin Chary }
1993e230dd2SCorentin Chary 
2003e230dd2SCorentin Chary static int vnc_server_info_put(QDict *qdict)
2013e230dd2SCorentin Chary {
2023e230dd2SCorentin Chary     if (vnc_server_addr_put(qdict, vnc_display->lsock) < 0) {
2033e230dd2SCorentin Chary         return -1;
2043e230dd2SCorentin Chary     }
2053e230dd2SCorentin Chary 
2063e230dd2SCorentin Chary     qdict_put(qdict, "auth", qstring_from_str(vnc_auth_name(vnc_display)));
2073e230dd2SCorentin Chary     return 0;
2083e230dd2SCorentin Chary }
2093e230dd2SCorentin Chary 
2103e230dd2SCorentin Chary static void vnc_client_cache_auth(VncState *client)
2113e230dd2SCorentin Chary {
2122ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
2133e230dd2SCorentin Chary     QDict *qdict;
2142ded6ad7SBlue Swirl #endif
2153e230dd2SCorentin Chary 
2163e230dd2SCorentin Chary     if (!client->info) {
2173e230dd2SCorentin Chary         return;
2183e230dd2SCorentin Chary     }
2193e230dd2SCorentin Chary 
2202ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
2213e230dd2SCorentin Chary     qdict = qobject_to_qdict(client->info);
2222ded6ad7SBlue Swirl #endif
2233e230dd2SCorentin Chary 
2243e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
2253e230dd2SCorentin Chary     if (client->tls.session &&
2263e230dd2SCorentin Chary         client->tls.dname) {
2273e230dd2SCorentin Chary         qdict_put(qdict, "x509_dname", qstring_from_str(client->tls.dname));
2283e230dd2SCorentin Chary     }
2293e230dd2SCorentin Chary #endif
2303e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
2313e230dd2SCorentin Chary     if (client->sasl.conn &&
2323e230dd2SCorentin Chary         client->sasl.username) {
2333e230dd2SCorentin Chary         qdict_put(qdict, "sasl_username",
2343e230dd2SCorentin Chary                   qstring_from_str(client->sasl.username));
2353e230dd2SCorentin Chary     }
2363e230dd2SCorentin Chary #endif
2373e230dd2SCorentin Chary }
2383e230dd2SCorentin Chary 
2393e230dd2SCorentin Chary static void vnc_client_cache_addr(VncState *client)
2403e230dd2SCorentin Chary {
2413e230dd2SCorentin Chary     QDict *qdict;
2423e230dd2SCorentin Chary 
2433e230dd2SCorentin Chary     qdict = qdict_new();
2443e230dd2SCorentin Chary     if (vnc_qdict_remote_addr(qdict, client->csock) < 0) {
2453e230dd2SCorentin Chary         QDECREF(qdict);
2463e230dd2SCorentin Chary         /* XXX: how to report the error? */
2473e230dd2SCorentin Chary         return;
2483e230dd2SCorentin Chary     }
2493e230dd2SCorentin Chary 
2503e230dd2SCorentin Chary     client->info = QOBJECT(qdict);
2513e230dd2SCorentin Chary }
2523e230dd2SCorentin Chary 
2533e230dd2SCorentin Chary static void vnc_qmp_event(VncState *vs, MonitorEvent event)
2543e230dd2SCorentin Chary {
2553e230dd2SCorentin Chary     QDict *server;
2563e230dd2SCorentin Chary     QObject *data;
2573e230dd2SCorentin Chary 
2583e230dd2SCorentin Chary     if (!vs->info) {
2593e230dd2SCorentin Chary         return;
2603e230dd2SCorentin Chary     }
2613e230dd2SCorentin Chary 
2623e230dd2SCorentin Chary     server = qdict_new();
2633e230dd2SCorentin Chary     if (vnc_server_info_put(server) < 0) {
2643e230dd2SCorentin Chary         QDECREF(server);
2653e230dd2SCorentin Chary         return;
2663e230dd2SCorentin Chary     }
2673e230dd2SCorentin Chary 
2683e230dd2SCorentin Chary     data = qobject_from_jsonf("{ 'client': %p, 'server': %p }",
2693e230dd2SCorentin Chary                               vs->info, QOBJECT(server));
2703e230dd2SCorentin Chary 
2713e230dd2SCorentin Chary     monitor_protocol_event(event, data);
2723e230dd2SCorentin Chary 
2733e230dd2SCorentin Chary     qobject_incref(vs->info);
2743e230dd2SCorentin Chary     qobject_decref(data);
2753e230dd2SCorentin Chary }
2763e230dd2SCorentin Chary 
2773e230dd2SCorentin Chary static void info_vnc_iter(QObject *obj, void *opaque)
2783e230dd2SCorentin Chary {
2793e230dd2SCorentin Chary     QDict *client;
2803e230dd2SCorentin Chary     Monitor *mon = opaque;
2813e230dd2SCorentin Chary 
2823e230dd2SCorentin Chary     client = qobject_to_qdict(obj);
2833e230dd2SCorentin Chary     monitor_printf(mon, "Client:\n");
2843e230dd2SCorentin Chary     monitor_printf(mon, "     address: %s:%s\n",
2853e230dd2SCorentin Chary                    qdict_get_str(client, "host"),
2863e230dd2SCorentin Chary                    qdict_get_str(client, "service"));
2873e230dd2SCorentin Chary 
2883e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
2893e230dd2SCorentin Chary     monitor_printf(mon, "  x509_dname: %s\n",
2903e230dd2SCorentin Chary         qdict_haskey(client, "x509_dname") ?
2913e230dd2SCorentin Chary         qdict_get_str(client, "x509_dname") : "none");
2923e230dd2SCorentin Chary #endif
2933e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
2943e230dd2SCorentin Chary     monitor_printf(mon, "    username: %s\n",
2953e230dd2SCorentin Chary         qdict_haskey(client, "sasl_username") ?
2963e230dd2SCorentin Chary         qdict_get_str(client, "sasl_username") : "none");
2973e230dd2SCorentin Chary #endif
2983e230dd2SCorentin Chary }
2993e230dd2SCorentin Chary 
3003e230dd2SCorentin Chary void do_info_vnc_print(Monitor *mon, const QObject *data)
3013e230dd2SCorentin Chary {
3023e230dd2SCorentin Chary     QDict *server;
3033e230dd2SCorentin Chary     QList *clients;
3043e230dd2SCorentin Chary 
3053e230dd2SCorentin Chary     server = qobject_to_qdict(data);
3063e230dd2SCorentin Chary     if (qdict_get_bool(server, "enabled") == 0) {
3073e230dd2SCorentin Chary         monitor_printf(mon, "Server: disabled\n");
3083e230dd2SCorentin Chary         return;
3093e230dd2SCorentin Chary     }
3103e230dd2SCorentin Chary 
3113e230dd2SCorentin Chary     monitor_printf(mon, "Server:\n");
3123e230dd2SCorentin Chary     monitor_printf(mon, "     address: %s:%s\n",
3133e230dd2SCorentin Chary                    qdict_get_str(server, "host"),
3143e230dd2SCorentin Chary                    qdict_get_str(server, "service"));
3153e230dd2SCorentin Chary     monitor_printf(mon, "        auth: %s\n", qdict_get_str(server, "auth"));
3163e230dd2SCorentin Chary 
3173e230dd2SCorentin Chary     clients = qdict_get_qlist(server, "clients");
3183e230dd2SCorentin Chary     if (qlist_empty(clients)) {
3193e230dd2SCorentin Chary         monitor_printf(mon, "Client: none\n");
3203e230dd2SCorentin Chary     } else {
3213e230dd2SCorentin Chary         qlist_iter(clients, info_vnc_iter, mon);
3223e230dd2SCorentin Chary     }
3233e230dd2SCorentin Chary }
3243e230dd2SCorentin Chary 
3253e230dd2SCorentin Chary void do_info_vnc(Monitor *mon, QObject **ret_data)
3263e230dd2SCorentin Chary {
3273e230dd2SCorentin Chary     if (vnc_display == NULL || vnc_display->display == NULL) {
3283e230dd2SCorentin Chary         *ret_data = qobject_from_jsonf("{ 'enabled': false }");
3293e230dd2SCorentin Chary     } else {
3303e230dd2SCorentin Chary         QList *clist;
3313e230dd2SCorentin Chary         VncState *client;
3323e230dd2SCorentin Chary 
3333e230dd2SCorentin Chary         clist = qlist_new();
3343e230dd2SCorentin Chary         QTAILQ_FOREACH(client, &vnc_display->clients, next) {
3353e230dd2SCorentin Chary             if (client->info) {
3363e230dd2SCorentin Chary                 /* incref so that it's not freed by upper layers */
3373e230dd2SCorentin Chary                 qobject_incref(client->info);
3383e230dd2SCorentin Chary                 qlist_append_obj(clist, client->info);
3393e230dd2SCorentin Chary             }
3403e230dd2SCorentin Chary         }
3413e230dd2SCorentin Chary 
3423e230dd2SCorentin Chary         *ret_data = qobject_from_jsonf("{ 'enabled': true, 'clients': %p }",
3433e230dd2SCorentin Chary                                        QOBJECT(clist));
3443e230dd2SCorentin Chary         assert(*ret_data != NULL);
3453e230dd2SCorentin Chary 
3463e230dd2SCorentin Chary         if (vnc_server_info_put(qobject_to_qdict(*ret_data)) < 0) {
3473e230dd2SCorentin Chary             qobject_decref(*ret_data);
3483e230dd2SCorentin Chary             *ret_data = NULL;
3493e230dd2SCorentin Chary         }
3503e230dd2SCorentin Chary     }
3513e230dd2SCorentin Chary }
3523e230dd2SCorentin Chary 
3533e230dd2SCorentin Chary /* TODO
3543e230dd2SCorentin Chary    1) Get the queue working for IO.
3553e230dd2SCorentin Chary    2) there is some weirdness when using the -S option (the screen is grey
3563e230dd2SCorentin Chary       and not totally invalidated
3573e230dd2SCorentin Chary    3) resolutions > 1024
3583e230dd2SCorentin Chary */
3593e230dd2SCorentin Chary 
3603e230dd2SCorentin Chary static int vnc_update_client(VncState *vs, int has_dirty);
361bd023f95SCorentin Chary static int vnc_update_client_sync(VncState *vs, int has_dirty);
3623e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs);
3633e230dd2SCorentin Chary static void vnc_disconnect_finish(VncState *vs);
3643e230dd2SCorentin Chary static void vnc_init_timer(VncDisplay *vd);
3653e230dd2SCorentin Chary static void vnc_remove_timer(VncDisplay *vd);
3663e230dd2SCorentin Chary 
3673e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs);
3683e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental,
3693e230dd2SCorentin Chary                                        int x_position, int y_position,
3703e230dd2SCorentin Chary                                        int w, int h);
3713e230dd2SCorentin Chary static void vnc_refresh(void *opaque);
3723e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd);
3733e230dd2SCorentin Chary 
3743e230dd2SCorentin Chary static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h)
3753e230dd2SCorentin Chary {
3763e230dd2SCorentin Chary     int i;
3773e230dd2SCorentin Chary     VncDisplay *vd = ds->opaque;
3783e230dd2SCorentin Chary     struct VncSurface *s = &vd->guest;
3793e230dd2SCorentin Chary 
3803e230dd2SCorentin Chary     h += y;
3813e230dd2SCorentin Chary 
3823e230dd2SCorentin Chary     /* round x down to ensure the loop only spans one 16-pixel block per,
3833e230dd2SCorentin Chary        iteration.  otherwise, if (x % 16) != 0, the last iteration may span
3843e230dd2SCorentin Chary        two 16-pixel blocks but we only mark the first as dirty
3853e230dd2SCorentin Chary     */
3863e230dd2SCorentin Chary     w += (x % 16);
3873e230dd2SCorentin Chary     x -= (x % 16);
3883e230dd2SCorentin Chary 
3893e230dd2SCorentin Chary     x = MIN(x, s->ds->width);
3903e230dd2SCorentin Chary     y = MIN(y, s->ds->height);
3913e230dd2SCorentin Chary     w = MIN(x + w, s->ds->width) - x;
3923e230dd2SCorentin Chary     h = MIN(h, s->ds->height);
3933e230dd2SCorentin Chary 
3943e230dd2SCorentin Chary     for (; y < h; y++)
3953e230dd2SCorentin Chary         for (i = 0; i < w; i += 16)
396bc2429b9SCorentin Chary             set_bit((x + i) / 16, s->dirty[y]);
3973e230dd2SCorentin Chary }
3983e230dd2SCorentin Chary 
3993e230dd2SCorentin Chary void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
4003e230dd2SCorentin Chary                             int32_t encoding)
4013e230dd2SCorentin Chary {
4023e230dd2SCorentin Chary     vnc_write_u16(vs, x);
4033e230dd2SCorentin Chary     vnc_write_u16(vs, y);
4043e230dd2SCorentin Chary     vnc_write_u16(vs, w);
4053e230dd2SCorentin Chary     vnc_write_u16(vs, h);
4063e230dd2SCorentin Chary 
4073e230dd2SCorentin Chary     vnc_write_s32(vs, encoding);
4083e230dd2SCorentin Chary }
4093e230dd2SCorentin Chary 
4103e230dd2SCorentin Chary void buffer_reserve(Buffer *buffer, size_t len)
4113e230dd2SCorentin Chary {
4123e230dd2SCorentin Chary     if ((buffer->capacity - buffer->offset) < len) {
4133e230dd2SCorentin Chary         buffer->capacity += (len + 1024);
4143e230dd2SCorentin Chary         buffer->buffer = qemu_realloc(buffer->buffer, buffer->capacity);
4153e230dd2SCorentin Chary         if (buffer->buffer == NULL) {
4163e230dd2SCorentin Chary             fprintf(stderr, "vnc: out of memory\n");
4173e230dd2SCorentin Chary             exit(1);
4183e230dd2SCorentin Chary         }
4193e230dd2SCorentin Chary     }
4203e230dd2SCorentin Chary }
4213e230dd2SCorentin Chary 
4223e230dd2SCorentin Chary int buffer_empty(Buffer *buffer)
4233e230dd2SCorentin Chary {
4243e230dd2SCorentin Chary     return buffer->offset == 0;
4253e230dd2SCorentin Chary }
4263e230dd2SCorentin Chary 
4273e230dd2SCorentin Chary uint8_t *buffer_end(Buffer *buffer)
4283e230dd2SCorentin Chary {
4293e230dd2SCorentin Chary     return buffer->buffer + buffer->offset;
4303e230dd2SCorentin Chary }
4313e230dd2SCorentin Chary 
4323e230dd2SCorentin Chary void buffer_reset(Buffer *buffer)
4333e230dd2SCorentin Chary {
4343e230dd2SCorentin Chary         buffer->offset = 0;
4353e230dd2SCorentin Chary }
4363e230dd2SCorentin Chary 
4373e230dd2SCorentin Chary void buffer_free(Buffer *buffer)
4383e230dd2SCorentin Chary {
4393e230dd2SCorentin Chary     qemu_free(buffer->buffer);
4403e230dd2SCorentin Chary     buffer->offset = 0;
4413e230dd2SCorentin Chary     buffer->capacity = 0;
4423e230dd2SCorentin Chary     buffer->buffer = NULL;
4433e230dd2SCorentin Chary }
4443e230dd2SCorentin Chary 
4453e230dd2SCorentin Chary void buffer_append(Buffer *buffer, const void *data, size_t len)
4463e230dd2SCorentin Chary {
4473e230dd2SCorentin Chary     memcpy(buffer->buffer + buffer->offset, data, len);
4483e230dd2SCorentin Chary     buffer->offset += len;
4493e230dd2SCorentin Chary }
4503e230dd2SCorentin Chary 
4513e230dd2SCorentin Chary static void vnc_desktop_resize(VncState *vs)
4523e230dd2SCorentin Chary {
4533e230dd2SCorentin Chary     DisplayState *ds = vs->ds;
4543e230dd2SCorentin Chary 
4553e230dd2SCorentin Chary     if (vs->csock == -1 || !vnc_has_feature(vs, VNC_FEATURE_RESIZE)) {
4563e230dd2SCorentin Chary         return;
4573e230dd2SCorentin Chary     }
4583e230dd2SCorentin Chary     if (vs->client_width == ds_get_width(ds) &&
4593e230dd2SCorentin Chary         vs->client_height == ds_get_height(ds)) {
4603e230dd2SCorentin Chary         return;
4613e230dd2SCorentin Chary     }
4623e230dd2SCorentin Chary     vs->client_width = ds_get_width(ds);
4633e230dd2SCorentin Chary     vs->client_height = ds_get_height(ds);
464bd023f95SCorentin Chary     vnc_lock_output(vs);
4653e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
4663e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
4673e230dd2SCorentin Chary     vnc_write_u16(vs, 1); /* number of rects */
4683e230dd2SCorentin Chary     vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height,
4693e230dd2SCorentin Chary                            VNC_ENCODING_DESKTOPRESIZE);
470bd023f95SCorentin Chary     vnc_unlock_output(vs);
4713e230dd2SCorentin Chary     vnc_flush(vs);
4723e230dd2SCorentin Chary }
4733e230dd2SCorentin Chary 
474bd023f95SCorentin Chary #ifdef CONFIG_VNC_THREAD
475bd023f95SCorentin Chary static void vnc_abort_display_jobs(VncDisplay *vd)
476bd023f95SCorentin Chary {
477bd023f95SCorentin Chary     VncState *vs;
478bd023f95SCorentin Chary 
479bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
480bd023f95SCorentin Chary         vnc_lock_output(vs);
481bd023f95SCorentin Chary         vs->abort = true;
482bd023f95SCorentin Chary         vnc_unlock_output(vs);
483bd023f95SCorentin Chary     }
484bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
485bd023f95SCorentin Chary         vnc_jobs_join(vs);
486bd023f95SCorentin Chary     }
487bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
488bd023f95SCorentin Chary         vnc_lock_output(vs);
489bd023f95SCorentin Chary         vs->abort = false;
490bd023f95SCorentin Chary         vnc_unlock_output(vs);
491bd023f95SCorentin Chary     }
492bd023f95SCorentin Chary }
493bd023f95SCorentin Chary #else
494bd023f95SCorentin Chary static void vnc_abort_display_jobs(VncDisplay *vd)
495bd023f95SCorentin Chary {
496bd023f95SCorentin Chary }
497bd023f95SCorentin Chary #endif
498bd023f95SCorentin Chary 
4993e230dd2SCorentin Chary static void vnc_dpy_resize(DisplayState *ds)
5003e230dd2SCorentin Chary {
5013e230dd2SCorentin Chary     VncDisplay *vd = ds->opaque;
5023e230dd2SCorentin Chary     VncState *vs;
5033e230dd2SCorentin Chary 
504bd023f95SCorentin Chary     vnc_abort_display_jobs(vd);
505bd023f95SCorentin Chary 
5063e230dd2SCorentin Chary     /* server surface */
5073e230dd2SCorentin Chary     if (!vd->server)
5083e230dd2SCorentin Chary         vd->server = qemu_mallocz(sizeof(*vd->server));
5093e230dd2SCorentin Chary     if (vd->server->data)
5103e230dd2SCorentin Chary         qemu_free(vd->server->data);
5113e230dd2SCorentin Chary     *(vd->server) = *(ds->surface);
5123e230dd2SCorentin Chary     vd->server->data = qemu_mallocz(vd->server->linesize *
5133e230dd2SCorentin Chary                                     vd->server->height);
5143e230dd2SCorentin Chary 
5153e230dd2SCorentin Chary     /* guest surface */
5163e230dd2SCorentin Chary     if (!vd->guest.ds)
5173e230dd2SCorentin Chary         vd->guest.ds = qemu_mallocz(sizeof(*vd->guest.ds));
5183e230dd2SCorentin Chary     if (ds_get_bytes_per_pixel(ds) != vd->guest.ds->pf.bytes_per_pixel)
5193e230dd2SCorentin Chary         console_color_init(ds);
5203e230dd2SCorentin Chary     *(vd->guest.ds) = *(ds->surface);
5213e230dd2SCorentin Chary     memset(vd->guest.dirty, 0xFF, sizeof(vd->guest.dirty));
5223e230dd2SCorentin Chary 
5233e230dd2SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
5243e230dd2SCorentin Chary         vnc_colordepth(vs);
5253e230dd2SCorentin Chary         vnc_desktop_resize(vs);
5263e230dd2SCorentin Chary         if (vs->vd->cursor) {
5273e230dd2SCorentin Chary             vnc_cursor_define(vs);
5283e230dd2SCorentin Chary         }
5293e230dd2SCorentin Chary         memset(vs->dirty, 0xFF, sizeof(vs->dirty));
5303e230dd2SCorentin Chary     }
5313e230dd2SCorentin Chary }
5323e230dd2SCorentin Chary 
5333e230dd2SCorentin Chary /* fastest code */
5343e230dd2SCorentin Chary static void vnc_write_pixels_copy(VncState *vs, struct PixelFormat *pf,
5353e230dd2SCorentin Chary                                   void *pixels, int size)
5363e230dd2SCorentin Chary {
5373e230dd2SCorentin Chary     vnc_write(vs, pixels, size);
5383e230dd2SCorentin Chary }
5393e230dd2SCorentin Chary 
5403e230dd2SCorentin Chary /* slowest but generic code. */
5413e230dd2SCorentin Chary void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v)
5423e230dd2SCorentin Chary {
5433e230dd2SCorentin Chary     uint8_t r, g, b;
5443e230dd2SCorentin Chary     VncDisplay *vd = vs->vd;
5453e230dd2SCorentin Chary 
5463e230dd2SCorentin Chary     r = ((((v & vd->server->pf.rmask) >> vd->server->pf.rshift) << vs->clientds.pf.rbits) >>
5473e230dd2SCorentin Chary         vd->server->pf.rbits);
5483e230dd2SCorentin Chary     g = ((((v & vd->server->pf.gmask) >> vd->server->pf.gshift) << vs->clientds.pf.gbits) >>
5493e230dd2SCorentin Chary         vd->server->pf.gbits);
5503e230dd2SCorentin Chary     b = ((((v & vd->server->pf.bmask) >> vd->server->pf.bshift) << vs->clientds.pf.bbits) >>
5513e230dd2SCorentin Chary         vd->server->pf.bbits);
5523e230dd2SCorentin Chary     v = (r << vs->clientds.pf.rshift) |
5533e230dd2SCorentin Chary         (g << vs->clientds.pf.gshift) |
5543e230dd2SCorentin Chary         (b << vs->clientds.pf.bshift);
5553e230dd2SCorentin Chary     switch(vs->clientds.pf.bytes_per_pixel) {
5563e230dd2SCorentin Chary     case 1:
5573e230dd2SCorentin Chary         buf[0] = v;
5583e230dd2SCorentin Chary         break;
5593e230dd2SCorentin Chary     case 2:
5603e230dd2SCorentin Chary         if (vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) {
5613e230dd2SCorentin Chary             buf[0] = v >> 8;
5623e230dd2SCorentin Chary             buf[1] = v;
5633e230dd2SCorentin Chary         } else {
5643e230dd2SCorentin Chary             buf[1] = v >> 8;
5653e230dd2SCorentin Chary             buf[0] = v;
5663e230dd2SCorentin Chary         }
5673e230dd2SCorentin Chary         break;
5683e230dd2SCorentin Chary     default:
5693e230dd2SCorentin Chary     case 4:
5703e230dd2SCorentin Chary         if (vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) {
5713e230dd2SCorentin Chary             buf[0] = v >> 24;
5723e230dd2SCorentin Chary             buf[1] = v >> 16;
5733e230dd2SCorentin Chary             buf[2] = v >> 8;
5743e230dd2SCorentin Chary             buf[3] = v;
5753e230dd2SCorentin Chary         } else {
5763e230dd2SCorentin Chary             buf[3] = v >> 24;
5773e230dd2SCorentin Chary             buf[2] = v >> 16;
5783e230dd2SCorentin Chary             buf[1] = v >> 8;
5793e230dd2SCorentin Chary             buf[0] = v;
5803e230dd2SCorentin Chary         }
5813e230dd2SCorentin Chary         break;
5823e230dd2SCorentin Chary     }
5833e230dd2SCorentin Chary }
5843e230dd2SCorentin Chary 
5853e230dd2SCorentin Chary static void vnc_write_pixels_generic(VncState *vs, struct PixelFormat *pf,
5863e230dd2SCorentin Chary                                      void *pixels1, int size)
5873e230dd2SCorentin Chary {
5883e230dd2SCorentin Chary     uint8_t buf[4];
5893e230dd2SCorentin Chary 
5903e230dd2SCorentin Chary     if (pf->bytes_per_pixel == 4) {
5913e230dd2SCorentin Chary         uint32_t *pixels = pixels1;
5923e230dd2SCorentin Chary         int n, i;
5933e230dd2SCorentin Chary         n = size >> 2;
5943e230dd2SCorentin Chary         for(i = 0; i < n; i++) {
5953e230dd2SCorentin Chary             vnc_convert_pixel(vs, buf, pixels[i]);
5963e230dd2SCorentin Chary             vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel);
5973e230dd2SCorentin Chary         }
5983e230dd2SCorentin Chary     } else if (pf->bytes_per_pixel == 2) {
5993e230dd2SCorentin Chary         uint16_t *pixels = pixels1;
6003e230dd2SCorentin Chary         int n, i;
6013e230dd2SCorentin Chary         n = size >> 1;
6023e230dd2SCorentin Chary         for(i = 0; i < n; i++) {
6033e230dd2SCorentin Chary             vnc_convert_pixel(vs, buf, pixels[i]);
6043e230dd2SCorentin Chary             vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel);
6053e230dd2SCorentin Chary         }
6063e230dd2SCorentin Chary     } else if (pf->bytes_per_pixel == 1) {
6073e230dd2SCorentin Chary         uint8_t *pixels = pixels1;
6083e230dd2SCorentin Chary         int n, i;
6093e230dd2SCorentin Chary         n = size;
6103e230dd2SCorentin Chary         for(i = 0; i < n; i++) {
6113e230dd2SCorentin Chary             vnc_convert_pixel(vs, buf, pixels[i]);
6123e230dd2SCorentin Chary             vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel);
6133e230dd2SCorentin Chary         }
6143e230dd2SCorentin Chary     } else {
6153e230dd2SCorentin Chary         fprintf(stderr, "vnc_write_pixels_generic: VncState color depth not supported\n");
6163e230dd2SCorentin Chary     }
6173e230dd2SCorentin Chary }
6183e230dd2SCorentin Chary 
6193e230dd2SCorentin Chary int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
6203e230dd2SCorentin Chary {
6213e230dd2SCorentin Chary     int i;
6223e230dd2SCorentin Chary     uint8_t *row;
6233e230dd2SCorentin Chary     VncDisplay *vd = vs->vd;
6243e230dd2SCorentin Chary 
6253e230dd2SCorentin Chary     row = vd->server->data + y * ds_get_linesize(vs->ds) + x * ds_get_bytes_per_pixel(vs->ds);
6263e230dd2SCorentin Chary     for (i = 0; i < h; i++) {
6273e230dd2SCorentin Chary         vs->write_pixels(vs, &vd->server->pf, row, w * ds_get_bytes_per_pixel(vs->ds));
6283e230dd2SCorentin Chary         row += ds_get_linesize(vs->ds);
6293e230dd2SCorentin Chary     }
6303e230dd2SCorentin Chary     return 1;
6313e230dd2SCorentin Chary }
6323e230dd2SCorentin Chary 
633bd023f95SCorentin Chary int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
6343e230dd2SCorentin Chary {
6353e230dd2SCorentin Chary     int n = 0;
6363e230dd2SCorentin Chary 
6373e230dd2SCorentin Chary     switch(vs->vnc_encoding) {
6383e230dd2SCorentin Chary         case VNC_ENCODING_ZLIB:
6393e230dd2SCorentin Chary             n = vnc_zlib_send_framebuffer_update(vs, x, y, w, h);
6403e230dd2SCorentin Chary             break;
6413e230dd2SCorentin Chary         case VNC_ENCODING_HEXTILE:
6423e230dd2SCorentin Chary             vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE);
6433e230dd2SCorentin Chary             n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h);
6443e230dd2SCorentin Chary             break;
6453e230dd2SCorentin Chary         case VNC_ENCODING_TIGHT:
6463e230dd2SCorentin Chary             n = vnc_tight_send_framebuffer_update(vs, x, y, w, h);
6473e230dd2SCorentin Chary             break;
648efe556adSCorentin Chary         case VNC_ENCODING_TIGHT_PNG:
649efe556adSCorentin Chary             n = vnc_tight_png_send_framebuffer_update(vs, x, y, w, h);
650efe556adSCorentin Chary             break;
651148954faSCorentin Chary         case VNC_ENCODING_ZRLE:
652148954faSCorentin Chary             n = vnc_zrle_send_framebuffer_update(vs, x, y, w, h);
653148954faSCorentin Chary             break;
654148954faSCorentin Chary         case VNC_ENCODING_ZYWRLE:
655148954faSCorentin Chary             n = vnc_zywrle_send_framebuffer_update(vs, x, y, w, h);
656148954faSCorentin Chary             break;
6573e230dd2SCorentin Chary         default:
6583e230dd2SCorentin Chary             vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW);
6593e230dd2SCorentin Chary             n = vnc_raw_send_framebuffer_update(vs, x, y, w, h);
6603e230dd2SCorentin Chary             break;
6613e230dd2SCorentin Chary     }
6623e230dd2SCorentin Chary     return n;
6633e230dd2SCorentin Chary }
6643e230dd2SCorentin Chary 
6653e230dd2SCorentin Chary static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
6663e230dd2SCorentin Chary {
6673e230dd2SCorentin Chary     /* send bitblit op to the vnc client */
668bd023f95SCorentin Chary     vnc_lock_output(vs);
6693e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
6703e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
6713e230dd2SCorentin Chary     vnc_write_u16(vs, 1); /* number of rects */
6723e230dd2SCorentin Chary     vnc_framebuffer_update(vs, dst_x, dst_y, w, h, VNC_ENCODING_COPYRECT);
6733e230dd2SCorentin Chary     vnc_write_u16(vs, src_x);
6743e230dd2SCorentin Chary     vnc_write_u16(vs, src_y);
675bd023f95SCorentin Chary     vnc_unlock_output(vs);
6763e230dd2SCorentin Chary     vnc_flush(vs);
6773e230dd2SCorentin Chary }
6783e230dd2SCorentin Chary 
6793e230dd2SCorentin Chary static void vnc_dpy_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
6803e230dd2SCorentin Chary {
6813e230dd2SCorentin Chary     VncDisplay *vd = ds->opaque;
6823e230dd2SCorentin Chary     VncState *vs, *vn;
6833e230dd2SCorentin Chary     uint8_t *src_row;
6843e230dd2SCorentin Chary     uint8_t *dst_row;
6853e230dd2SCorentin Chary     int i,x,y,pitch,depth,inc,w_lim,s;
6863e230dd2SCorentin Chary     int cmp_bytes;
6873e230dd2SCorentin Chary 
6883e230dd2SCorentin Chary     vnc_refresh_server_surface(vd);
6893e230dd2SCorentin Chary     QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
6903e230dd2SCorentin Chary         if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
6913e230dd2SCorentin Chary             vs->force_update = 1;
692bd023f95SCorentin Chary             vnc_update_client_sync(vs, 1);
6933e230dd2SCorentin Chary             /* vs might be free()ed here */
6943e230dd2SCorentin Chary         }
6953e230dd2SCorentin Chary     }
6963e230dd2SCorentin Chary 
6973e230dd2SCorentin Chary     /* do bitblit op on the local surface too */
6983e230dd2SCorentin Chary     pitch = ds_get_linesize(vd->ds);
6993e230dd2SCorentin Chary     depth = ds_get_bytes_per_pixel(vd->ds);
7003e230dd2SCorentin Chary     src_row = vd->server->data + pitch * src_y + depth * src_x;
7013e230dd2SCorentin Chary     dst_row = vd->server->data + pitch * dst_y + depth * dst_x;
7023e230dd2SCorentin Chary     y = dst_y;
7033e230dd2SCorentin Chary     inc = 1;
7043e230dd2SCorentin Chary     if (dst_y > src_y) {
7053e230dd2SCorentin Chary         /* copy backwards */
7063e230dd2SCorentin Chary         src_row += pitch * (h-1);
7073e230dd2SCorentin Chary         dst_row += pitch * (h-1);
7083e230dd2SCorentin Chary         pitch = -pitch;
7093e230dd2SCorentin Chary         y = dst_y + h - 1;
7103e230dd2SCorentin Chary         inc = -1;
7113e230dd2SCorentin Chary     }
7123e230dd2SCorentin Chary     w_lim = w - (16 - (dst_x % 16));
7133e230dd2SCorentin Chary     if (w_lim < 0)
7143e230dd2SCorentin Chary         w_lim = w;
7153e230dd2SCorentin Chary     else
7163e230dd2SCorentin Chary         w_lim = w - (w_lim % 16);
7173e230dd2SCorentin Chary     for (i = 0; i < h; i++) {
7183e230dd2SCorentin Chary         for (x = 0; x <= w_lim;
7193e230dd2SCorentin Chary                 x += s, src_row += cmp_bytes, dst_row += cmp_bytes) {
7203e230dd2SCorentin Chary             if (x == w_lim) {
7213e230dd2SCorentin Chary                 if ((s = w - w_lim) == 0)
7223e230dd2SCorentin Chary                     break;
7233e230dd2SCorentin Chary             } else if (!x) {
7243e230dd2SCorentin Chary                 s = (16 - (dst_x % 16));
7253e230dd2SCorentin Chary                 s = MIN(s, w_lim);
7263e230dd2SCorentin Chary             } else {
7273e230dd2SCorentin Chary                 s = 16;
7283e230dd2SCorentin Chary             }
7293e230dd2SCorentin Chary             cmp_bytes = s * depth;
7303e230dd2SCorentin Chary             if (memcmp(src_row, dst_row, cmp_bytes) == 0)
7313e230dd2SCorentin Chary                 continue;
7323e230dd2SCorentin Chary             memmove(dst_row, src_row, cmp_bytes);
7333e230dd2SCorentin Chary             QTAILQ_FOREACH(vs, &vd->clients, next) {
7343e230dd2SCorentin Chary                 if (!vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
735bc2429b9SCorentin Chary                     set_bit(((x + dst_x) / 16), vs->dirty[y]);
7363e230dd2SCorentin Chary                 }
7373e230dd2SCorentin Chary             }
7383e230dd2SCorentin Chary         }
7393e230dd2SCorentin Chary         src_row += pitch - w * depth;
7403e230dd2SCorentin Chary         dst_row += pitch - w * depth;
7413e230dd2SCorentin Chary         y += inc;
7423e230dd2SCorentin Chary     }
7433e230dd2SCorentin Chary 
7443e230dd2SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
7453e230dd2SCorentin Chary         if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
7463e230dd2SCorentin Chary             vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h);
7473e230dd2SCorentin Chary         }
7483e230dd2SCorentin Chary     }
7493e230dd2SCorentin Chary }
7503e230dd2SCorentin Chary 
7513e230dd2SCorentin Chary static void vnc_mouse_set(int x, int y, int visible)
7523e230dd2SCorentin Chary {
7533e230dd2SCorentin Chary     /* can we ask the client(s) to move the pointer ??? */
7543e230dd2SCorentin Chary }
7553e230dd2SCorentin Chary 
7563e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs)
7573e230dd2SCorentin Chary {
7583e230dd2SCorentin Chary     QEMUCursor *c = vs->vd->cursor;
7593e230dd2SCorentin Chary     PixelFormat pf = qemu_default_pixelformat(32);
7603e230dd2SCorentin Chary     int isize;
7613e230dd2SCorentin Chary 
7623e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) {
763d01f9595SCorentin Chary         vnc_lock_output(vs);
7643e230dd2SCorentin Chary         vnc_write_u8(vs,  VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
7653e230dd2SCorentin Chary         vnc_write_u8(vs,  0);  /*  padding     */
7663e230dd2SCorentin Chary         vnc_write_u16(vs, 1);  /*  # of rects  */
7673e230dd2SCorentin Chary         vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height,
7683e230dd2SCorentin Chary                                VNC_ENCODING_RICH_CURSOR);
7693e230dd2SCorentin Chary         isize = c->width * c->height * vs->clientds.pf.bytes_per_pixel;
7703e230dd2SCorentin Chary         vnc_write_pixels_generic(vs, &pf, c->data, isize);
7713e230dd2SCorentin Chary         vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize);
772d01f9595SCorentin Chary         vnc_unlock_output(vs);
7733e230dd2SCorentin Chary         return 0;
7743e230dd2SCorentin Chary     }
7753e230dd2SCorentin Chary     return -1;
7763e230dd2SCorentin Chary }
7773e230dd2SCorentin Chary 
7783e230dd2SCorentin Chary static void vnc_dpy_cursor_define(QEMUCursor *c)
7793e230dd2SCorentin Chary {
7803e230dd2SCorentin Chary     VncDisplay *vd = vnc_display;
7813e230dd2SCorentin Chary     VncState *vs;
7823e230dd2SCorentin Chary 
7833e230dd2SCorentin Chary     cursor_put(vd->cursor);
7843e230dd2SCorentin Chary     qemu_free(vd->cursor_mask);
7853e230dd2SCorentin Chary 
7863e230dd2SCorentin Chary     vd->cursor = c;
7873e230dd2SCorentin Chary     cursor_get(vd->cursor);
7883e230dd2SCorentin Chary     vd->cursor_msize = cursor_get_mono_bpl(c) * c->height;
7893e230dd2SCorentin Chary     vd->cursor_mask = qemu_mallocz(vd->cursor_msize);
7903e230dd2SCorentin Chary     cursor_get_mono_mask(c, 0, vd->cursor_mask);
7913e230dd2SCorentin Chary 
7923e230dd2SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
7933e230dd2SCorentin Chary         vnc_cursor_define(vs);
7943e230dd2SCorentin Chary     }
7953e230dd2SCorentin Chary }
7963e230dd2SCorentin Chary 
7973e230dd2SCorentin Chary static int find_and_clear_dirty_height(struct VncState *vs,
798*6c71a539SCorentin Chary                                        int y, int last_x, int x, int height)
7993e230dd2SCorentin Chary {
8003e230dd2SCorentin Chary     int h;
8013e230dd2SCorentin Chary 
802*6c71a539SCorentin Chary     for (h = 1; h < (height - y); h++) {
8033e230dd2SCorentin Chary         int tmp_x;
804bc2429b9SCorentin Chary         if (!test_bit(last_x, vs->dirty[y + h])) {
8053e230dd2SCorentin Chary             break;
806bc2429b9SCorentin Chary         }
807bc2429b9SCorentin Chary         for (tmp_x = last_x; tmp_x < x; tmp_x++) {
808bc2429b9SCorentin Chary             clear_bit(tmp_x, vs->dirty[y + h]);
809bc2429b9SCorentin Chary         }
8103e230dd2SCorentin Chary     }
8113e230dd2SCorentin Chary 
8123e230dd2SCorentin Chary     return h;
8133e230dd2SCorentin Chary }
8143e230dd2SCorentin Chary 
815bd023f95SCorentin Chary #ifdef CONFIG_VNC_THREAD
816bd023f95SCorentin Chary static int vnc_update_client_sync(VncState *vs, int has_dirty)
817bd023f95SCorentin Chary {
818bd023f95SCorentin Chary     int ret = vnc_update_client(vs, has_dirty);
819bd023f95SCorentin Chary     vnc_jobs_join(vs);
820bd023f95SCorentin Chary     return ret;
821bd023f95SCorentin Chary }
822bd023f95SCorentin Chary #else
823bd023f95SCorentin Chary static int vnc_update_client_sync(VncState *vs, int has_dirty)
824bd023f95SCorentin Chary {
825bd023f95SCorentin Chary     return vnc_update_client(vs, has_dirty);
826bd023f95SCorentin Chary }
827bd023f95SCorentin Chary #endif
828bd023f95SCorentin Chary 
8293e230dd2SCorentin Chary static int vnc_update_client(VncState *vs, int has_dirty)
8303e230dd2SCorentin Chary {
8313e230dd2SCorentin Chary     if (vs->need_update && vs->csock != -1) {
8323e230dd2SCorentin Chary         VncDisplay *vd = vs->vd;
833bd023f95SCorentin Chary         VncJob *job;
8343e230dd2SCorentin Chary         int y;
8353e230dd2SCorentin Chary         int width, height;
836bd023f95SCorentin Chary         int n = 0;
837bd023f95SCorentin Chary 
8383e230dd2SCorentin Chary 
8393e230dd2SCorentin Chary         if (vs->output.offset && !vs->audio_cap && !vs->force_update)
8403e230dd2SCorentin Chary             /* kernel send buffers are full -> drop frames to throttle */
8413e230dd2SCorentin Chary             return 0;
8423e230dd2SCorentin Chary 
8433e230dd2SCorentin Chary         if (!has_dirty && !vs->audio_cap && !vs->force_update)
8443e230dd2SCorentin Chary             return 0;
8453e230dd2SCorentin Chary 
8463e230dd2SCorentin Chary         /*
8473e230dd2SCorentin Chary          * Send screen updates to the vnc client using the server
8483e230dd2SCorentin Chary          * surface and server dirty map.  guest surface updates
8493e230dd2SCorentin Chary          * happening in parallel don't disturb us, the next pass will
8503e230dd2SCorentin Chary          * send them to the client.
8513e230dd2SCorentin Chary          */
852bd023f95SCorentin Chary         job = vnc_job_new(vs);
8533e230dd2SCorentin Chary 
8543e230dd2SCorentin Chary         width = MIN(vd->server->width, vs->client_width);
8553e230dd2SCorentin Chary         height = MIN(vd->server->height, vs->client_height);
8563e230dd2SCorentin Chary 
8573e230dd2SCorentin Chary         for (y = 0; y < height; y++) {
8583e230dd2SCorentin Chary             int x;
8593e230dd2SCorentin Chary             int last_x = -1;
8603e230dd2SCorentin Chary             for (x = 0; x < width / 16; x++) {
861bc2429b9SCorentin Chary                 if (test_and_clear_bit(x, vs->dirty[y])) {
8623e230dd2SCorentin Chary                     if (last_x == -1) {
8633e230dd2SCorentin Chary                         last_x = x;
8643e230dd2SCorentin Chary                     }
8653e230dd2SCorentin Chary                 } else {
8663e230dd2SCorentin Chary                     if (last_x != -1) {
867*6c71a539SCorentin Chary                         int h = find_and_clear_dirty_height(vs, y, last_x, x,
868*6c71a539SCorentin Chary                                                             height);
869bd023f95SCorentin Chary 
870bd023f95SCorentin Chary                         n += vnc_job_add_rect(job, last_x * 16, y,
8713e230dd2SCorentin Chary                                               (x - last_x) * 16, h);
8723e230dd2SCorentin Chary                     }
8733e230dd2SCorentin Chary                     last_x = -1;
8743e230dd2SCorentin Chary                 }
8753e230dd2SCorentin Chary             }
8763e230dd2SCorentin Chary             if (last_x != -1) {
877*6c71a539SCorentin Chary                 int h = find_and_clear_dirty_height(vs, y, last_x, x, height);
878bd023f95SCorentin Chary                 n += vnc_job_add_rect(job, last_x * 16, y,
8793e230dd2SCorentin Chary                                       (x - last_x) * 16, h);
8803e230dd2SCorentin Chary             }
8813e230dd2SCorentin Chary         }
882bd023f95SCorentin Chary 
883bd023f95SCorentin Chary         vnc_job_push(job);
8843e230dd2SCorentin Chary         vs->force_update = 0;
885bd023f95SCorentin Chary         return n;
8863e230dd2SCorentin Chary     }
8873e230dd2SCorentin Chary 
8883e230dd2SCorentin Chary     if (vs->csock == -1)
8893e230dd2SCorentin Chary         vnc_disconnect_finish(vs);
8903e230dd2SCorentin Chary 
8913e230dd2SCorentin Chary     return 0;
8923e230dd2SCorentin Chary }
8933e230dd2SCorentin Chary 
8943e230dd2SCorentin Chary /* audio */
8953e230dd2SCorentin Chary static void audio_capture_notify(void *opaque, audcnotification_e cmd)
8963e230dd2SCorentin Chary {
8973e230dd2SCorentin Chary     VncState *vs = opaque;
8983e230dd2SCorentin Chary 
8993e230dd2SCorentin Chary     switch (cmd) {
9003e230dd2SCorentin Chary     case AUD_CNOTIFY_DISABLE:
901bd023f95SCorentin Chary         vnc_lock_output(vs);
9023e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
9033e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
9043e230dd2SCorentin Chary         vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END);
905bd023f95SCorentin Chary         vnc_unlock_output(vs);
9063e230dd2SCorentin Chary         vnc_flush(vs);
9073e230dd2SCorentin Chary         break;
9083e230dd2SCorentin Chary 
9093e230dd2SCorentin Chary     case AUD_CNOTIFY_ENABLE:
910bd023f95SCorentin Chary         vnc_lock_output(vs);
9113e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
9123e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
9133e230dd2SCorentin Chary         vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN);
914bd023f95SCorentin Chary         vnc_unlock_output(vs);
9153e230dd2SCorentin Chary         vnc_flush(vs);
9163e230dd2SCorentin Chary         break;
9173e230dd2SCorentin Chary     }
9183e230dd2SCorentin Chary }
9193e230dd2SCorentin Chary 
9203e230dd2SCorentin Chary static void audio_capture_destroy(void *opaque)
9213e230dd2SCorentin Chary {
9223e230dd2SCorentin Chary }
9233e230dd2SCorentin Chary 
9243e230dd2SCorentin Chary static void audio_capture(void *opaque, void *buf, int size)
9253e230dd2SCorentin Chary {
9263e230dd2SCorentin Chary     VncState *vs = opaque;
9273e230dd2SCorentin Chary 
928bd023f95SCorentin Chary     vnc_lock_output(vs);
9293e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
9303e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
9313e230dd2SCorentin Chary     vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA);
9323e230dd2SCorentin Chary     vnc_write_u32(vs, size);
9333e230dd2SCorentin Chary     vnc_write(vs, buf, size);
934bd023f95SCorentin Chary     vnc_unlock_output(vs);
9353e230dd2SCorentin Chary     vnc_flush(vs);
9363e230dd2SCorentin Chary }
9373e230dd2SCorentin Chary 
9383e230dd2SCorentin Chary static void audio_add(VncState *vs)
9393e230dd2SCorentin Chary {
9403e230dd2SCorentin Chary     struct audio_capture_ops ops;
9413e230dd2SCorentin Chary 
9423e230dd2SCorentin Chary     if (vs->audio_cap) {
9433e230dd2SCorentin Chary         monitor_printf(default_mon, "audio already running\n");
9443e230dd2SCorentin Chary         return;
9453e230dd2SCorentin Chary     }
9463e230dd2SCorentin Chary 
9473e230dd2SCorentin Chary     ops.notify = audio_capture_notify;
9483e230dd2SCorentin Chary     ops.destroy = audio_capture_destroy;
9493e230dd2SCorentin Chary     ops.capture = audio_capture;
9503e230dd2SCorentin Chary 
9513e230dd2SCorentin Chary     vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs);
9523e230dd2SCorentin Chary     if (!vs->audio_cap) {
9533e230dd2SCorentin Chary         monitor_printf(default_mon, "Failed to add audio capture\n");
9543e230dd2SCorentin Chary     }
9553e230dd2SCorentin Chary }
9563e230dd2SCorentin Chary 
9573e230dd2SCorentin Chary static void audio_del(VncState *vs)
9583e230dd2SCorentin Chary {
9593e230dd2SCorentin Chary     if (vs->audio_cap) {
9603e230dd2SCorentin Chary         AUD_del_capture(vs->audio_cap, vs);
9613e230dd2SCorentin Chary         vs->audio_cap = NULL;
9623e230dd2SCorentin Chary     }
9633e230dd2SCorentin Chary }
9643e230dd2SCorentin Chary 
9653e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs)
9663e230dd2SCorentin Chary {
9673e230dd2SCorentin Chary     if (vs->csock == -1)
9683e230dd2SCorentin Chary         return;
9693e230dd2SCorentin Chary     qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
9703e230dd2SCorentin Chary     closesocket(vs->csock);
9713e230dd2SCorentin Chary     vs->csock = -1;
9723e230dd2SCorentin Chary }
9733e230dd2SCorentin Chary 
9743e230dd2SCorentin Chary static void vnc_disconnect_finish(VncState *vs)
9753e230dd2SCorentin Chary {
9767d964c9dSCorentin Chary     int i;
9777d964c9dSCorentin Chary 
978bd023f95SCorentin Chary     vnc_jobs_join(vs); /* Wait encoding jobs */
979bd023f95SCorentin Chary 
980bd023f95SCorentin Chary     vnc_lock_output(vs);
9813e230dd2SCorentin Chary     vnc_qmp_event(vs, QEVENT_VNC_DISCONNECTED);
9823e230dd2SCorentin Chary 
9833e230dd2SCorentin Chary     buffer_free(&vs->input);
9843e230dd2SCorentin Chary     buffer_free(&vs->output);
9853e230dd2SCorentin Chary 
9863e230dd2SCorentin Chary     qobject_decref(vs->info);
9873e230dd2SCorentin Chary 
9883e230dd2SCorentin Chary     vnc_zlib_clear(vs);
9893e230dd2SCorentin Chary     vnc_tight_clear(vs);
990148954faSCorentin Chary     vnc_zrle_clear(vs);
9913e230dd2SCorentin Chary 
9923e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
9933e230dd2SCorentin Chary     vnc_tls_client_cleanup(vs);
9943e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
9953e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
9963e230dd2SCorentin Chary     vnc_sasl_client_cleanup(vs);
9973e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
9983e230dd2SCorentin Chary     audio_del(vs);
9993e230dd2SCorentin Chary 
10003e230dd2SCorentin Chary     QTAILQ_REMOVE(&vs->vd->clients, vs, next);
10013e230dd2SCorentin Chary 
10023e230dd2SCorentin Chary     if (QTAILQ_EMPTY(&vs->vd->clients)) {
10033e230dd2SCorentin Chary         dcl->idle = 1;
10043e230dd2SCorentin Chary     }
10053e230dd2SCorentin Chary 
10063e230dd2SCorentin Chary     qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
10073e230dd2SCorentin Chary     vnc_remove_timer(vs->vd);
10083e230dd2SCorentin Chary     if (vs->vd->lock_key_sync)
10093e230dd2SCorentin Chary         qemu_remove_led_event_handler(vs->led);
1010bd023f95SCorentin Chary     vnc_unlock_output(vs);
1011bd023f95SCorentin Chary 
1012bd023f95SCorentin Chary #ifdef CONFIG_VNC_THREAD
1013bd023f95SCorentin Chary     qemu_mutex_destroy(&vs->output_mutex);
1014bd023f95SCorentin Chary #endif
10157d964c9dSCorentin Chary     for (i = 0; i < VNC_STAT_ROWS; ++i) {
10167d964c9dSCorentin Chary         qemu_free(vs->lossy_rect[i]);
10177d964c9dSCorentin Chary     }
10187d964c9dSCorentin Chary     qemu_free(vs->lossy_rect);
10193e230dd2SCorentin Chary     qemu_free(vs);
10203e230dd2SCorentin Chary }
10213e230dd2SCorentin Chary 
10223e230dd2SCorentin Chary int vnc_client_io_error(VncState *vs, int ret, int last_errno)
10233e230dd2SCorentin Chary {
10243e230dd2SCorentin Chary     if (ret == 0 || ret == -1) {
10253e230dd2SCorentin Chary         if (ret == -1) {
10263e230dd2SCorentin Chary             switch (last_errno) {
10273e230dd2SCorentin Chary                 case EINTR:
10283e230dd2SCorentin Chary                 case EAGAIN:
10293e230dd2SCorentin Chary #ifdef _WIN32
10303e230dd2SCorentin Chary                 case WSAEWOULDBLOCK:
10313e230dd2SCorentin Chary #endif
10323e230dd2SCorentin Chary                     return 0;
10333e230dd2SCorentin Chary                 default:
10343e230dd2SCorentin Chary                     break;
10353e230dd2SCorentin Chary             }
10363e230dd2SCorentin Chary         }
10373e230dd2SCorentin Chary 
10383e230dd2SCorentin Chary         VNC_DEBUG("Closing down client sock: ret %d, errno %d\n",
10393e230dd2SCorentin Chary                   ret, ret < 0 ? last_errno : 0);
10403e230dd2SCorentin Chary         vnc_disconnect_start(vs);
10413e230dd2SCorentin Chary 
10423e230dd2SCorentin Chary         return 0;
10433e230dd2SCorentin Chary     }
10443e230dd2SCorentin Chary     return ret;
10453e230dd2SCorentin Chary }
10463e230dd2SCorentin Chary 
10473e230dd2SCorentin Chary 
10483e230dd2SCorentin Chary void vnc_client_error(VncState *vs)
10493e230dd2SCorentin Chary {
10503e230dd2SCorentin Chary     VNC_DEBUG("Closing down client sock: protocol error\n");
10513e230dd2SCorentin Chary     vnc_disconnect_start(vs);
10523e230dd2SCorentin Chary }
10533e230dd2SCorentin Chary 
10543e230dd2SCorentin Chary 
10553e230dd2SCorentin Chary /*
10563e230dd2SCorentin Chary  * Called to write a chunk of data to the client socket. The data may
10573e230dd2SCorentin Chary  * be the raw data, or may have already been encoded by SASL.
10583e230dd2SCorentin Chary  * The data will be written either straight onto the socket, or
10593e230dd2SCorentin Chary  * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
10603e230dd2SCorentin Chary  *
10613e230dd2SCorentin Chary  * NB, it is theoretically possible to have 2 layers of encryption,
10623e230dd2SCorentin Chary  * both SASL, and this TLS layer. It is highly unlikely in practice
10633e230dd2SCorentin Chary  * though, since SASL encryption will typically be a no-op if TLS
10643e230dd2SCorentin Chary  * is active
10653e230dd2SCorentin Chary  *
10663e230dd2SCorentin Chary  * Returns the number of bytes written, which may be less than
10673e230dd2SCorentin Chary  * the requested 'datalen' if the socket would block. Returns
10683e230dd2SCorentin Chary  * -1 on error, and disconnects the client socket.
10693e230dd2SCorentin Chary  */
10703e230dd2SCorentin Chary long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
10713e230dd2SCorentin Chary {
10723e230dd2SCorentin Chary     long ret;
10733e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
10743e230dd2SCorentin Chary     if (vs->tls.session) {
10753e230dd2SCorentin Chary         ret = gnutls_write(vs->tls.session, data, datalen);
10763e230dd2SCorentin Chary         if (ret < 0) {
10773e230dd2SCorentin Chary             if (ret == GNUTLS_E_AGAIN)
10783e230dd2SCorentin Chary                 errno = EAGAIN;
10793e230dd2SCorentin Chary             else
10803e230dd2SCorentin Chary                 errno = EIO;
10813e230dd2SCorentin Chary             ret = -1;
10823e230dd2SCorentin Chary         }
10833e230dd2SCorentin Chary     } else
10843e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
10853e230dd2SCorentin Chary         ret = send(vs->csock, (const void *)data, datalen, 0);
10863e230dd2SCorentin Chary     VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret);
10873e230dd2SCorentin Chary     return vnc_client_io_error(vs, ret, socket_error());
10883e230dd2SCorentin Chary }
10893e230dd2SCorentin Chary 
10903e230dd2SCorentin Chary 
10913e230dd2SCorentin Chary /*
10923e230dd2SCorentin Chary  * Called to write buffered data to the client socket, when not
10933e230dd2SCorentin Chary  * using any SASL SSF encryption layers. Will write as much data
10943e230dd2SCorentin Chary  * as possible without blocking. If all buffered data is written,
10953e230dd2SCorentin Chary  * will switch the FD poll() handler back to read monitoring.
10963e230dd2SCorentin Chary  *
10973e230dd2SCorentin Chary  * Returns the number of bytes written, which may be less than
10983e230dd2SCorentin Chary  * the buffered output data if the socket would block. Returns
10993e230dd2SCorentin Chary  * -1 on error, and disconnects the client socket.
11003e230dd2SCorentin Chary  */
11013e230dd2SCorentin Chary static long vnc_client_write_plain(VncState *vs)
11023e230dd2SCorentin Chary {
11033e230dd2SCorentin Chary     long ret;
11043e230dd2SCorentin Chary 
11053e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
11063e230dd2SCorentin Chary     VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
11073e230dd2SCorentin Chary               vs->output.buffer, vs->output.capacity, vs->output.offset,
11083e230dd2SCorentin Chary               vs->sasl.waitWriteSSF);
11093e230dd2SCorentin Chary 
11103e230dd2SCorentin Chary     if (vs->sasl.conn &&
11113e230dd2SCorentin Chary         vs->sasl.runSSF &&
11123e230dd2SCorentin Chary         vs->sasl.waitWriteSSF) {
11133e230dd2SCorentin Chary         ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF);
11143e230dd2SCorentin Chary         if (ret)
11153e230dd2SCorentin Chary             vs->sasl.waitWriteSSF -= ret;
11163e230dd2SCorentin Chary     } else
11173e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
11183e230dd2SCorentin Chary         ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset);
11193e230dd2SCorentin Chary     if (!ret)
11203e230dd2SCorentin Chary         return 0;
11213e230dd2SCorentin Chary 
11223e230dd2SCorentin Chary     memmove(vs->output.buffer, vs->output.buffer + ret, (vs->output.offset - ret));
11233e230dd2SCorentin Chary     vs->output.offset -= ret;
11243e230dd2SCorentin Chary 
11253e230dd2SCorentin Chary     if (vs->output.offset == 0) {
11263e230dd2SCorentin Chary         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
11273e230dd2SCorentin Chary     }
11283e230dd2SCorentin Chary 
11293e230dd2SCorentin Chary     return ret;
11303e230dd2SCorentin Chary }
11313e230dd2SCorentin Chary 
11323e230dd2SCorentin Chary 
11333e230dd2SCorentin Chary /*
11343e230dd2SCorentin Chary  * First function called whenever there is data to be written to
11353e230dd2SCorentin Chary  * the client socket. Will delegate actual work according to whether
11363e230dd2SCorentin Chary  * SASL SSF layers are enabled (thus requiring encryption calls)
11373e230dd2SCorentin Chary  */
1138bd023f95SCorentin Chary static void vnc_client_write_locked(void *opaque)
11393e230dd2SCorentin Chary {
11403e230dd2SCorentin Chary     VncState *vs = opaque;
11413e230dd2SCorentin Chary 
11423e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
11433e230dd2SCorentin Chary     if (vs->sasl.conn &&
11443e230dd2SCorentin Chary         vs->sasl.runSSF &&
11453e230dd2SCorentin Chary         !vs->sasl.waitWriteSSF) {
11463e230dd2SCorentin Chary         vnc_client_write_sasl(vs);
11473e230dd2SCorentin Chary     } else
11483e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
11493e230dd2SCorentin Chary         vnc_client_write_plain(vs);
11503e230dd2SCorentin Chary }
11513e230dd2SCorentin Chary 
1152bd023f95SCorentin Chary void vnc_client_write(void *opaque)
1153bd023f95SCorentin Chary {
1154bd023f95SCorentin Chary     VncState *vs = opaque;
1155bd023f95SCorentin Chary 
1156bd023f95SCorentin Chary     vnc_lock_output(vs);
1157bd023f95SCorentin Chary     if (vs->output.offset) {
1158bd023f95SCorentin Chary         vnc_client_write_locked(opaque);
1159ac71103dSYoshiaki Tamura     } else if (vs->csock != -1) {
1160bd023f95SCorentin Chary         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
1161bd023f95SCorentin Chary     }
1162bd023f95SCorentin Chary     vnc_unlock_output(vs);
1163bd023f95SCorentin Chary }
1164bd023f95SCorentin Chary 
11653e230dd2SCorentin Chary void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
11663e230dd2SCorentin Chary {
11673e230dd2SCorentin Chary     vs->read_handler = func;
11683e230dd2SCorentin Chary     vs->read_handler_expect = expecting;
11693e230dd2SCorentin Chary }
11703e230dd2SCorentin Chary 
11713e230dd2SCorentin Chary 
11723e230dd2SCorentin Chary /*
11733e230dd2SCorentin Chary  * Called to read a chunk of data from the client socket. The data may
11743e230dd2SCorentin Chary  * be the raw data, or may need to be further decoded by SASL.
11753e230dd2SCorentin Chary  * The data will be read either straight from to the socket, or
11763e230dd2SCorentin Chary  * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
11773e230dd2SCorentin Chary  *
11783e230dd2SCorentin Chary  * NB, it is theoretically possible to have 2 layers of encryption,
11793e230dd2SCorentin Chary  * both SASL, and this TLS layer. It is highly unlikely in practice
11803e230dd2SCorentin Chary  * though, since SASL encryption will typically be a no-op if TLS
11813e230dd2SCorentin Chary  * is active
11823e230dd2SCorentin Chary  *
11833e230dd2SCorentin Chary  * Returns the number of bytes read, which may be less than
11843e230dd2SCorentin Chary  * the requested 'datalen' if the socket would block. Returns
11853e230dd2SCorentin Chary  * -1 on error, and disconnects the client socket.
11863e230dd2SCorentin Chary  */
11873e230dd2SCorentin Chary long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
11883e230dd2SCorentin Chary {
11893e230dd2SCorentin Chary     long ret;
11903e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
11913e230dd2SCorentin Chary     if (vs->tls.session) {
11923e230dd2SCorentin Chary         ret = gnutls_read(vs->tls.session, data, datalen);
11933e230dd2SCorentin Chary         if (ret < 0) {
11943e230dd2SCorentin Chary             if (ret == GNUTLS_E_AGAIN)
11953e230dd2SCorentin Chary                 errno = EAGAIN;
11963e230dd2SCorentin Chary             else
11973e230dd2SCorentin Chary                 errno = EIO;
11983e230dd2SCorentin Chary             ret = -1;
11993e230dd2SCorentin Chary         }
12003e230dd2SCorentin Chary     } else
12013e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
12023e230dd2SCorentin Chary         ret = recv(vs->csock, (void *)data, datalen, 0);
12033e230dd2SCorentin Chary     VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret);
12043e230dd2SCorentin Chary     return vnc_client_io_error(vs, ret, socket_error());
12053e230dd2SCorentin Chary }
12063e230dd2SCorentin Chary 
12073e230dd2SCorentin Chary 
12083e230dd2SCorentin Chary /*
12093e230dd2SCorentin Chary  * Called to read data from the client socket to the input buffer,
12103e230dd2SCorentin Chary  * when not using any SASL SSF encryption layers. Will read as much
12113e230dd2SCorentin Chary  * data as possible without blocking.
12123e230dd2SCorentin Chary  *
12133e230dd2SCorentin Chary  * Returns the number of bytes read. Returns -1 on error, and
12143e230dd2SCorentin Chary  * disconnects the client socket.
12153e230dd2SCorentin Chary  */
12163e230dd2SCorentin Chary static long vnc_client_read_plain(VncState *vs)
12173e230dd2SCorentin Chary {
12183e230dd2SCorentin Chary     int ret;
12193e230dd2SCorentin Chary     VNC_DEBUG("Read plain %p size %zd offset %zd\n",
12203e230dd2SCorentin Chary               vs->input.buffer, vs->input.capacity, vs->input.offset);
12213e230dd2SCorentin Chary     buffer_reserve(&vs->input, 4096);
12223e230dd2SCorentin Chary     ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096);
12233e230dd2SCorentin Chary     if (!ret)
12243e230dd2SCorentin Chary         return 0;
12253e230dd2SCorentin Chary     vs->input.offset += ret;
12263e230dd2SCorentin Chary     return ret;
12273e230dd2SCorentin Chary }
12283e230dd2SCorentin Chary 
12293e230dd2SCorentin Chary 
12303e230dd2SCorentin Chary /*
12313e230dd2SCorentin Chary  * First function called whenever there is more data to be read from
12323e230dd2SCorentin Chary  * the client socket. Will delegate actual work according to whether
12333e230dd2SCorentin Chary  * SASL SSF layers are enabled (thus requiring decryption calls)
12343e230dd2SCorentin Chary  */
12353e230dd2SCorentin Chary void vnc_client_read(void *opaque)
12363e230dd2SCorentin Chary {
12373e230dd2SCorentin Chary     VncState *vs = opaque;
12383e230dd2SCorentin Chary     long ret;
12393e230dd2SCorentin Chary 
12403e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
12413e230dd2SCorentin Chary     if (vs->sasl.conn && vs->sasl.runSSF)
12423e230dd2SCorentin Chary         ret = vnc_client_read_sasl(vs);
12433e230dd2SCorentin Chary     else
12443e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
12453e230dd2SCorentin Chary         ret = vnc_client_read_plain(vs);
12463e230dd2SCorentin Chary     if (!ret) {
12473e230dd2SCorentin Chary         if (vs->csock == -1)
12483e230dd2SCorentin Chary             vnc_disconnect_finish(vs);
12493e230dd2SCorentin Chary         return;
12503e230dd2SCorentin Chary     }
12513e230dd2SCorentin Chary 
12523e230dd2SCorentin Chary     while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
12533e230dd2SCorentin Chary         size_t len = vs->read_handler_expect;
12543e230dd2SCorentin Chary         int ret;
12553e230dd2SCorentin Chary 
12563e230dd2SCorentin Chary         ret = vs->read_handler(vs, vs->input.buffer, len);
12573e230dd2SCorentin Chary         if (vs->csock == -1) {
12583e230dd2SCorentin Chary             vnc_disconnect_finish(vs);
12593e230dd2SCorentin Chary             return;
12603e230dd2SCorentin Chary         }
12613e230dd2SCorentin Chary 
12623e230dd2SCorentin Chary         if (!ret) {
12633e230dd2SCorentin Chary             memmove(vs->input.buffer, vs->input.buffer + len, (vs->input.offset - len));
12643e230dd2SCorentin Chary             vs->input.offset -= len;
12653e230dd2SCorentin Chary         } else {
12663e230dd2SCorentin Chary             vs->read_handler_expect = ret;
12673e230dd2SCorentin Chary         }
12683e230dd2SCorentin Chary     }
12693e230dd2SCorentin Chary }
12703e230dd2SCorentin Chary 
12713e230dd2SCorentin Chary void vnc_write(VncState *vs, const void *data, size_t len)
12723e230dd2SCorentin Chary {
12733e230dd2SCorentin Chary     buffer_reserve(&vs->output, len);
12743e230dd2SCorentin Chary 
12753e230dd2SCorentin Chary     if (vs->csock != -1 && buffer_empty(&vs->output)) {
12763e230dd2SCorentin Chary         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
12773e230dd2SCorentin Chary     }
12783e230dd2SCorentin Chary 
12793e230dd2SCorentin Chary     buffer_append(&vs->output, data, len);
12803e230dd2SCorentin Chary }
12813e230dd2SCorentin Chary 
12823e230dd2SCorentin Chary void vnc_write_s32(VncState *vs, int32_t value)
12833e230dd2SCorentin Chary {
12843e230dd2SCorentin Chary     vnc_write_u32(vs, *(uint32_t *)&value);
12853e230dd2SCorentin Chary }
12863e230dd2SCorentin Chary 
12873e230dd2SCorentin Chary void vnc_write_u32(VncState *vs, uint32_t value)
12883e230dd2SCorentin Chary {
12893e230dd2SCorentin Chary     uint8_t buf[4];
12903e230dd2SCorentin Chary 
12913e230dd2SCorentin Chary     buf[0] = (value >> 24) & 0xFF;
12923e230dd2SCorentin Chary     buf[1] = (value >> 16) & 0xFF;
12933e230dd2SCorentin Chary     buf[2] = (value >>  8) & 0xFF;
12943e230dd2SCorentin Chary     buf[3] = value & 0xFF;
12953e230dd2SCorentin Chary 
12963e230dd2SCorentin Chary     vnc_write(vs, buf, 4);
12973e230dd2SCorentin Chary }
12983e230dd2SCorentin Chary 
12993e230dd2SCorentin Chary void vnc_write_u16(VncState *vs, uint16_t value)
13003e230dd2SCorentin Chary {
13013e230dd2SCorentin Chary     uint8_t buf[2];
13023e230dd2SCorentin Chary 
13033e230dd2SCorentin Chary     buf[0] = (value >> 8) & 0xFF;
13043e230dd2SCorentin Chary     buf[1] = value & 0xFF;
13053e230dd2SCorentin Chary 
13063e230dd2SCorentin Chary     vnc_write(vs, buf, 2);
13073e230dd2SCorentin Chary }
13083e230dd2SCorentin Chary 
13093e230dd2SCorentin Chary void vnc_write_u8(VncState *vs, uint8_t value)
13103e230dd2SCorentin Chary {
13113e230dd2SCorentin Chary     vnc_write(vs, (char *)&value, 1);
13123e230dd2SCorentin Chary }
13133e230dd2SCorentin Chary 
13143e230dd2SCorentin Chary void vnc_flush(VncState *vs)
13153e230dd2SCorentin Chary {
1316bd023f95SCorentin Chary     vnc_lock_output(vs);
1317bd023f95SCorentin Chary     if (vs->csock != -1 && vs->output.offset) {
1318bd023f95SCorentin Chary         vnc_client_write_locked(vs);
1319bd023f95SCorentin Chary     }
1320bd023f95SCorentin Chary     vnc_unlock_output(vs);
13213e230dd2SCorentin Chary }
13223e230dd2SCorentin Chary 
13233e230dd2SCorentin Chary uint8_t read_u8(uint8_t *data, size_t offset)
13243e230dd2SCorentin Chary {
13253e230dd2SCorentin Chary     return data[offset];
13263e230dd2SCorentin Chary }
13273e230dd2SCorentin Chary 
13283e230dd2SCorentin Chary uint16_t read_u16(uint8_t *data, size_t offset)
13293e230dd2SCorentin Chary {
13303e230dd2SCorentin Chary     return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
13313e230dd2SCorentin Chary }
13323e230dd2SCorentin Chary 
13333e230dd2SCorentin Chary int32_t read_s32(uint8_t *data, size_t offset)
13343e230dd2SCorentin Chary {
13353e230dd2SCorentin Chary     return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
13363e230dd2SCorentin Chary                      (data[offset + 2] << 8) | data[offset + 3]);
13373e230dd2SCorentin Chary }
13383e230dd2SCorentin Chary 
13393e230dd2SCorentin Chary uint32_t read_u32(uint8_t *data, size_t offset)
13403e230dd2SCorentin Chary {
13413e230dd2SCorentin Chary     return ((data[offset] << 24) | (data[offset + 1] << 16) |
13423e230dd2SCorentin Chary             (data[offset + 2] << 8) | data[offset + 3]);
13433e230dd2SCorentin Chary }
13443e230dd2SCorentin Chary 
13453e230dd2SCorentin Chary static void client_cut_text(VncState *vs, size_t len, uint8_t *text)
13463e230dd2SCorentin Chary {
13473e230dd2SCorentin Chary }
13483e230dd2SCorentin Chary 
13493e230dd2SCorentin Chary static void check_pointer_type_change(Notifier *notifier)
13503e230dd2SCorentin Chary {
13513e230dd2SCorentin Chary     VncState *vs = container_of(notifier, VncState, mouse_mode_notifier);
13523e230dd2SCorentin Chary     int absolute = kbd_mouse_is_absolute();
13533e230dd2SCorentin Chary 
13543e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) {
1355bd023f95SCorentin Chary         vnc_lock_output(vs);
13563e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
13573e230dd2SCorentin Chary         vnc_write_u8(vs, 0);
13583e230dd2SCorentin Chary         vnc_write_u16(vs, 1);
13593e230dd2SCorentin Chary         vnc_framebuffer_update(vs, absolute, 0,
13603e230dd2SCorentin Chary                                ds_get_width(vs->ds), ds_get_height(vs->ds),
13613e230dd2SCorentin Chary                                VNC_ENCODING_POINTER_TYPE_CHANGE);
1362bd023f95SCorentin Chary         vnc_unlock_output(vs);
13633e230dd2SCorentin Chary         vnc_flush(vs);
13643e230dd2SCorentin Chary     }
13653e230dd2SCorentin Chary     vs->absolute = absolute;
13663e230dd2SCorentin Chary }
13673e230dd2SCorentin Chary 
13683e230dd2SCorentin Chary static void pointer_event(VncState *vs, int button_mask, int x, int y)
13693e230dd2SCorentin Chary {
13703e230dd2SCorentin Chary     int buttons = 0;
13713e230dd2SCorentin Chary     int dz = 0;
13723e230dd2SCorentin Chary 
13733e230dd2SCorentin Chary     if (button_mask & 0x01)
13743e230dd2SCorentin Chary         buttons |= MOUSE_EVENT_LBUTTON;
13753e230dd2SCorentin Chary     if (button_mask & 0x02)
13763e230dd2SCorentin Chary         buttons |= MOUSE_EVENT_MBUTTON;
13773e230dd2SCorentin Chary     if (button_mask & 0x04)
13783e230dd2SCorentin Chary         buttons |= MOUSE_EVENT_RBUTTON;
13793e230dd2SCorentin Chary     if (button_mask & 0x08)
13803e230dd2SCorentin Chary         dz = -1;
13813e230dd2SCorentin Chary     if (button_mask & 0x10)
13823e230dd2SCorentin Chary         dz = 1;
13833e230dd2SCorentin Chary 
13843e230dd2SCorentin Chary     if (vs->absolute) {
13853e230dd2SCorentin Chary         kbd_mouse_event(ds_get_width(vs->ds) > 1 ?
13863e230dd2SCorentin Chary                           x * 0x7FFF / (ds_get_width(vs->ds) - 1) : 0x4000,
13873e230dd2SCorentin Chary                         ds_get_height(vs->ds) > 1 ?
13883e230dd2SCorentin Chary                           y * 0x7FFF / (ds_get_height(vs->ds) - 1) : 0x4000,
13893e230dd2SCorentin Chary                         dz, buttons);
13903e230dd2SCorentin Chary     } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) {
13913e230dd2SCorentin Chary         x -= 0x7FFF;
13923e230dd2SCorentin Chary         y -= 0x7FFF;
13933e230dd2SCorentin Chary 
13943e230dd2SCorentin Chary         kbd_mouse_event(x, y, dz, buttons);
13953e230dd2SCorentin Chary     } else {
13963e230dd2SCorentin Chary         if (vs->last_x != -1)
13973e230dd2SCorentin Chary             kbd_mouse_event(x - vs->last_x,
13983e230dd2SCorentin Chary                             y - vs->last_y,
13993e230dd2SCorentin Chary                             dz, buttons);
14003e230dd2SCorentin Chary         vs->last_x = x;
14013e230dd2SCorentin Chary         vs->last_y = y;
14023e230dd2SCorentin Chary     }
14033e230dd2SCorentin Chary }
14043e230dd2SCorentin Chary 
14053e230dd2SCorentin Chary static void reset_keys(VncState *vs)
14063e230dd2SCorentin Chary {
14073e230dd2SCorentin Chary     int i;
14083e230dd2SCorentin Chary     for(i = 0; i < 256; i++) {
14093e230dd2SCorentin Chary         if (vs->modifiers_state[i]) {
14103e230dd2SCorentin Chary             if (i & SCANCODE_GREY)
14113e230dd2SCorentin Chary                 kbd_put_keycode(SCANCODE_EMUL0);
14123e230dd2SCorentin Chary             kbd_put_keycode(i | SCANCODE_UP);
14133e230dd2SCorentin Chary             vs->modifiers_state[i] = 0;
14143e230dd2SCorentin Chary         }
14153e230dd2SCorentin Chary     }
14163e230dd2SCorentin Chary }
14173e230dd2SCorentin Chary 
14183e230dd2SCorentin Chary static void press_key(VncState *vs, int keysym)
14193e230dd2SCorentin Chary {
14203e230dd2SCorentin Chary     int keycode = keysym2scancode(vs->vd->kbd_layout, keysym) & SCANCODE_KEYMASK;
14213e230dd2SCorentin Chary     if (keycode & SCANCODE_GREY)
14223e230dd2SCorentin Chary         kbd_put_keycode(SCANCODE_EMUL0);
14233e230dd2SCorentin Chary     kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK);
14243e230dd2SCorentin Chary     if (keycode & SCANCODE_GREY)
14253e230dd2SCorentin Chary         kbd_put_keycode(SCANCODE_EMUL0);
14263e230dd2SCorentin Chary     kbd_put_keycode(keycode | SCANCODE_UP);
14273e230dd2SCorentin Chary }
14283e230dd2SCorentin Chary 
14293e230dd2SCorentin Chary static void kbd_leds(void *opaque, int ledstate)
14303e230dd2SCorentin Chary {
14313e230dd2SCorentin Chary     VncState *vs = opaque;
14323e230dd2SCorentin Chary     int caps, num;
14333e230dd2SCorentin Chary 
14343e230dd2SCorentin Chary     caps = ledstate & QEMU_CAPS_LOCK_LED ? 1 : 0;
14353e230dd2SCorentin Chary     num  = ledstate & QEMU_NUM_LOCK_LED  ? 1 : 0;
14363e230dd2SCorentin Chary 
14373e230dd2SCorentin Chary     if (vs->modifiers_state[0x3a] != caps) {
14383e230dd2SCorentin Chary         vs->modifiers_state[0x3a] = caps;
14393e230dd2SCorentin Chary     }
14403e230dd2SCorentin Chary     if (vs->modifiers_state[0x45] != num) {
14413e230dd2SCorentin Chary         vs->modifiers_state[0x45] = num;
14423e230dd2SCorentin Chary     }
14433e230dd2SCorentin Chary }
14443e230dd2SCorentin Chary 
14453e230dd2SCorentin Chary static void do_key_event(VncState *vs, int down, int keycode, int sym)
14463e230dd2SCorentin Chary {
14473e230dd2SCorentin Chary     /* QEMU console switch */
14483e230dd2SCorentin Chary     switch(keycode) {
14493e230dd2SCorentin Chary     case 0x2a:                          /* Left Shift */
14503e230dd2SCorentin Chary     case 0x36:                          /* Right Shift */
14513e230dd2SCorentin Chary     case 0x1d:                          /* Left CTRL */
14523e230dd2SCorentin Chary     case 0x9d:                          /* Right CTRL */
14533e230dd2SCorentin Chary     case 0x38:                          /* Left ALT */
14543e230dd2SCorentin Chary     case 0xb8:                          /* Right ALT */
14553e230dd2SCorentin Chary         if (down)
14563e230dd2SCorentin Chary             vs->modifiers_state[keycode] = 1;
14573e230dd2SCorentin Chary         else
14583e230dd2SCorentin Chary             vs->modifiers_state[keycode] = 0;
14593e230dd2SCorentin Chary         break;
14603e230dd2SCorentin Chary     case 0x02 ... 0x0a: /* '1' to '9' keys */
14613e230dd2SCorentin Chary         if (down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) {
14623e230dd2SCorentin Chary             /* Reset the modifiers sent to the current console */
14633e230dd2SCorentin Chary             reset_keys(vs);
14643e230dd2SCorentin Chary             console_select(keycode - 0x02);
14653e230dd2SCorentin Chary             return;
14663e230dd2SCorentin Chary         }
14673e230dd2SCorentin Chary         break;
14683e230dd2SCorentin Chary     case 0x3a:                        /* CapsLock */
14693e230dd2SCorentin Chary     case 0x45:                        /* NumLock */
14703e230dd2SCorentin Chary         if (down)
14713e230dd2SCorentin Chary             vs->modifiers_state[keycode] ^= 1;
14723e230dd2SCorentin Chary         break;
14733e230dd2SCorentin Chary     }
14743e230dd2SCorentin Chary 
14759892088bSGerd Hoffmann     if (down && vs->vd->lock_key_sync &&
14763e230dd2SCorentin Chary         keycode_is_keypad(vs->vd->kbd_layout, keycode)) {
14773e230dd2SCorentin Chary         /* If the numlock state needs to change then simulate an additional
14783e230dd2SCorentin Chary            keypress before sending this one.  This will happen if the user
14793e230dd2SCorentin Chary            toggles numlock away from the VNC window.
14803e230dd2SCorentin Chary         */
14813e230dd2SCorentin Chary         if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) {
14823e230dd2SCorentin Chary             if (!vs->modifiers_state[0x45]) {
14833e230dd2SCorentin Chary                 vs->modifiers_state[0x45] = 1;
14843e230dd2SCorentin Chary                 press_key(vs, 0xff7f);
14853e230dd2SCorentin Chary             }
14863e230dd2SCorentin Chary         } else {
14873e230dd2SCorentin Chary             if (vs->modifiers_state[0x45]) {
14883e230dd2SCorentin Chary                 vs->modifiers_state[0x45] = 0;
14893e230dd2SCorentin Chary                 press_key(vs, 0xff7f);
14903e230dd2SCorentin Chary             }
14913e230dd2SCorentin Chary         }
14923e230dd2SCorentin Chary     }
14933e230dd2SCorentin Chary 
14949892088bSGerd Hoffmann     if (down && vs->vd->lock_key_sync &&
14953e230dd2SCorentin Chary         ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) {
14963e230dd2SCorentin Chary         /* If the capslock state needs to change then simulate an additional
14973e230dd2SCorentin Chary            keypress before sending this one.  This will happen if the user
14983e230dd2SCorentin Chary            toggles capslock away from the VNC window.
14993e230dd2SCorentin Chary         */
15003e230dd2SCorentin Chary         int uppercase = !!(sym >= 'A' && sym <= 'Z');
15013e230dd2SCorentin Chary         int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]);
15023e230dd2SCorentin Chary         int capslock = !!(vs->modifiers_state[0x3a]);
15033e230dd2SCorentin Chary         if (capslock) {
15043e230dd2SCorentin Chary             if (uppercase == shift) {
15053e230dd2SCorentin Chary                 vs->modifiers_state[0x3a] = 0;
15063e230dd2SCorentin Chary                 press_key(vs, 0xffe5);
15073e230dd2SCorentin Chary             }
15083e230dd2SCorentin Chary         } else {
15093e230dd2SCorentin Chary             if (uppercase != shift) {
15103e230dd2SCorentin Chary                 vs->modifiers_state[0x3a] = 1;
15113e230dd2SCorentin Chary                 press_key(vs, 0xffe5);
15123e230dd2SCorentin Chary             }
15133e230dd2SCorentin Chary         }
15143e230dd2SCorentin Chary     }
15153e230dd2SCorentin Chary 
15163e230dd2SCorentin Chary     if (is_graphic_console()) {
15173e230dd2SCorentin Chary         if (keycode & SCANCODE_GREY)
15183e230dd2SCorentin Chary             kbd_put_keycode(SCANCODE_EMUL0);
15193e230dd2SCorentin Chary         if (down)
15203e230dd2SCorentin Chary             kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK);
15213e230dd2SCorentin Chary         else
15223e230dd2SCorentin Chary             kbd_put_keycode(keycode | SCANCODE_UP);
15233e230dd2SCorentin Chary     } else {
15243e230dd2SCorentin Chary         /* QEMU console emulation */
15253e230dd2SCorentin Chary         if (down) {
15263e230dd2SCorentin Chary             int numlock = vs->modifiers_state[0x45];
15273e230dd2SCorentin Chary             switch (keycode) {
15283e230dd2SCorentin Chary             case 0x2a:                          /* Left Shift */
15293e230dd2SCorentin Chary             case 0x36:                          /* Right Shift */
15303e230dd2SCorentin Chary             case 0x1d:                          /* Left CTRL */
15313e230dd2SCorentin Chary             case 0x9d:                          /* Right CTRL */
15323e230dd2SCorentin Chary             case 0x38:                          /* Left ALT */
15333e230dd2SCorentin Chary             case 0xb8:                          /* Right ALT */
15343e230dd2SCorentin Chary                 break;
15353e230dd2SCorentin Chary             case 0xc8:
15363e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_UP);
15373e230dd2SCorentin Chary                 break;
15383e230dd2SCorentin Chary             case 0xd0:
15393e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_DOWN);
15403e230dd2SCorentin Chary                 break;
15413e230dd2SCorentin Chary             case 0xcb:
15423e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_LEFT);
15433e230dd2SCorentin Chary                 break;
15443e230dd2SCorentin Chary             case 0xcd:
15453e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_RIGHT);
15463e230dd2SCorentin Chary                 break;
15473e230dd2SCorentin Chary             case 0xd3:
15483e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_DELETE);
15493e230dd2SCorentin Chary                 break;
15503e230dd2SCorentin Chary             case 0xc7:
15513e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_HOME);
15523e230dd2SCorentin Chary                 break;
15533e230dd2SCorentin Chary             case 0xcf:
15543e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_END);
15553e230dd2SCorentin Chary                 break;
15563e230dd2SCorentin Chary             case 0xc9:
15573e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_PAGEUP);
15583e230dd2SCorentin Chary                 break;
15593e230dd2SCorentin Chary             case 0xd1:
15603e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_PAGEDOWN);
15613e230dd2SCorentin Chary                 break;
15623e230dd2SCorentin Chary 
15633e230dd2SCorentin Chary             case 0x47:
15643e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME);
15653e230dd2SCorentin Chary                 break;
15663e230dd2SCorentin Chary             case 0x48:
15673e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP);
15683e230dd2SCorentin Chary                 break;
15693e230dd2SCorentin Chary             case 0x49:
15703e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP);
15713e230dd2SCorentin Chary                 break;
15723e230dd2SCorentin Chary             case 0x4b:
15733e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT);
15743e230dd2SCorentin Chary                 break;
15753e230dd2SCorentin Chary             case 0x4c:
15763e230dd2SCorentin Chary                 kbd_put_keysym('5');
15773e230dd2SCorentin Chary                 break;
15783e230dd2SCorentin Chary             case 0x4d:
15793e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT);
15803e230dd2SCorentin Chary                 break;
15813e230dd2SCorentin Chary             case 0x4f:
15823e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '1' : QEMU_KEY_END);
15833e230dd2SCorentin Chary                 break;
15843e230dd2SCorentin Chary             case 0x50:
15853e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN);
15863e230dd2SCorentin Chary                 break;
15873e230dd2SCorentin Chary             case 0x51:
15883e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN);
15893e230dd2SCorentin Chary                 break;
15903e230dd2SCorentin Chary             case 0x52:
15913e230dd2SCorentin Chary                 kbd_put_keysym('0');
15923e230dd2SCorentin Chary                 break;
15933e230dd2SCorentin Chary             case 0x53:
15943e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE);
15953e230dd2SCorentin Chary                 break;
15963e230dd2SCorentin Chary 
15973e230dd2SCorentin Chary             case 0xb5:
15983e230dd2SCorentin Chary                 kbd_put_keysym('/');
15993e230dd2SCorentin Chary                 break;
16003e230dd2SCorentin Chary             case 0x37:
16013e230dd2SCorentin Chary                 kbd_put_keysym('*');
16023e230dd2SCorentin Chary                 break;
16033e230dd2SCorentin Chary             case 0x4a:
16043e230dd2SCorentin Chary                 kbd_put_keysym('-');
16053e230dd2SCorentin Chary                 break;
16063e230dd2SCorentin Chary             case 0x4e:
16073e230dd2SCorentin Chary                 kbd_put_keysym('+');
16083e230dd2SCorentin Chary                 break;
16093e230dd2SCorentin Chary             case 0x9c:
16103e230dd2SCorentin Chary                 kbd_put_keysym('\n');
16113e230dd2SCorentin Chary                 break;
16123e230dd2SCorentin Chary 
16133e230dd2SCorentin Chary             default:
16143e230dd2SCorentin Chary                 kbd_put_keysym(sym);
16153e230dd2SCorentin Chary                 break;
16163e230dd2SCorentin Chary             }
16173e230dd2SCorentin Chary         }
16183e230dd2SCorentin Chary     }
16193e230dd2SCorentin Chary }
16203e230dd2SCorentin Chary 
16213e230dd2SCorentin Chary static void key_event(VncState *vs, int down, uint32_t sym)
16223e230dd2SCorentin Chary {
16233e230dd2SCorentin Chary     int keycode;
16243e230dd2SCorentin Chary     int lsym = sym;
16253e230dd2SCorentin Chary 
16263e230dd2SCorentin Chary     if (lsym >= 'A' && lsym <= 'Z' && is_graphic_console()) {
16273e230dd2SCorentin Chary         lsym = lsym - 'A' + 'a';
16283e230dd2SCorentin Chary     }
16293e230dd2SCorentin Chary 
16303e230dd2SCorentin Chary     keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF) & SCANCODE_KEYMASK;
16313e230dd2SCorentin Chary     do_key_event(vs, down, keycode, sym);
16323e230dd2SCorentin Chary }
16333e230dd2SCorentin Chary 
16343e230dd2SCorentin Chary static void ext_key_event(VncState *vs, int down,
16353e230dd2SCorentin Chary                           uint32_t sym, uint16_t keycode)
16363e230dd2SCorentin Chary {
16373e230dd2SCorentin Chary     /* if the user specifies a keyboard layout, always use it */
16383e230dd2SCorentin Chary     if (keyboard_layout)
16393e230dd2SCorentin Chary         key_event(vs, down, sym);
16403e230dd2SCorentin Chary     else
16413e230dd2SCorentin Chary         do_key_event(vs, down, keycode, sym);
16423e230dd2SCorentin Chary }
16433e230dd2SCorentin Chary 
16443e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental,
16453e230dd2SCorentin Chary                                        int x_position, int y_position,
16463e230dd2SCorentin Chary                                        int w, int h)
16473e230dd2SCorentin Chary {
16483e230dd2SCorentin Chary     if (y_position > ds_get_height(vs->ds))
16493e230dd2SCorentin Chary         y_position = ds_get_height(vs->ds);
16503e230dd2SCorentin Chary     if (y_position + h >= ds_get_height(vs->ds))
16513e230dd2SCorentin Chary         h = ds_get_height(vs->ds) - y_position;
16523e230dd2SCorentin Chary 
16533e230dd2SCorentin Chary     int i;
16543e230dd2SCorentin Chary     vs->need_update = 1;
16553e230dd2SCorentin Chary     if (!incremental) {
16563e230dd2SCorentin Chary         vs->force_update = 1;
16573e230dd2SCorentin Chary         for (i = 0; i < h; i++) {
1658bc2429b9SCorentin Chary             bitmap_set(vs->dirty[y_position + i], x_position / 16, w / 16);
16593e230dd2SCorentin Chary         }
16603e230dd2SCorentin Chary     }
16613e230dd2SCorentin Chary }
16623e230dd2SCorentin Chary 
16633e230dd2SCorentin Chary static void send_ext_key_event_ack(VncState *vs)
16643e230dd2SCorentin Chary {
1665bd023f95SCorentin Chary     vnc_lock_output(vs);
16663e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
16673e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
16683e230dd2SCorentin Chary     vnc_write_u16(vs, 1);
16693e230dd2SCorentin Chary     vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds), ds_get_height(vs->ds),
16703e230dd2SCorentin Chary                            VNC_ENCODING_EXT_KEY_EVENT);
1671bd023f95SCorentin Chary     vnc_unlock_output(vs);
16723e230dd2SCorentin Chary     vnc_flush(vs);
16733e230dd2SCorentin Chary }
16743e230dd2SCorentin Chary 
16753e230dd2SCorentin Chary static void send_ext_audio_ack(VncState *vs)
16763e230dd2SCorentin Chary {
1677bd023f95SCorentin Chary     vnc_lock_output(vs);
16783e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
16793e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
16803e230dd2SCorentin Chary     vnc_write_u16(vs, 1);
16813e230dd2SCorentin Chary     vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds), ds_get_height(vs->ds),
16823e230dd2SCorentin Chary                            VNC_ENCODING_AUDIO);
1683bd023f95SCorentin Chary     vnc_unlock_output(vs);
16843e230dd2SCorentin Chary     vnc_flush(vs);
16853e230dd2SCorentin Chary }
16863e230dd2SCorentin Chary 
16873e230dd2SCorentin Chary static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
16883e230dd2SCorentin Chary {
16893e230dd2SCorentin Chary     int i;
16903e230dd2SCorentin Chary     unsigned int enc = 0;
16913e230dd2SCorentin Chary 
16923e230dd2SCorentin Chary     vs->features = 0;
16933e230dd2SCorentin Chary     vs->vnc_encoding = 0;
1694d1af0e05SCorentin Chary     vs->tight.compression = 9;
1695d1af0e05SCorentin Chary     vs->tight.quality = -1; /* Lossless by default */
16963e230dd2SCorentin Chary     vs->absolute = -1;
16973e230dd2SCorentin Chary 
16983e230dd2SCorentin Chary     /*
16993e230dd2SCorentin Chary      * Start from the end because the encodings are sent in order of preference.
17003e230dd2SCorentin Chary      * This way the prefered encoding (first encoding defined in the array)
17013e230dd2SCorentin Chary      * will be set at the end of the loop.
17023e230dd2SCorentin Chary      */
17033e230dd2SCorentin Chary     for (i = n_encodings - 1; i >= 0; i--) {
17043e230dd2SCorentin Chary         enc = encodings[i];
17053e230dd2SCorentin Chary         switch (enc) {
17063e230dd2SCorentin Chary         case VNC_ENCODING_RAW:
17073e230dd2SCorentin Chary             vs->vnc_encoding = enc;
17083e230dd2SCorentin Chary             break;
17093e230dd2SCorentin Chary         case VNC_ENCODING_COPYRECT:
17103e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_COPYRECT_MASK;
17113e230dd2SCorentin Chary             break;
17123e230dd2SCorentin Chary         case VNC_ENCODING_HEXTILE:
17133e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_HEXTILE_MASK;
17143e230dd2SCorentin Chary             vs->vnc_encoding = enc;
17153e230dd2SCorentin Chary             break;
17163e230dd2SCorentin Chary         case VNC_ENCODING_TIGHT:
17173e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_TIGHT_MASK;
17183e230dd2SCorentin Chary             vs->vnc_encoding = enc;
17193e230dd2SCorentin Chary             break;
1720efe556adSCorentin Chary         case VNC_ENCODING_TIGHT_PNG:
1721efe556adSCorentin Chary             vs->features |= VNC_FEATURE_TIGHT_PNG_MASK;
1722efe556adSCorentin Chary             vs->vnc_encoding = enc;
1723efe556adSCorentin Chary             break;
17243e230dd2SCorentin Chary         case VNC_ENCODING_ZLIB:
17253e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_ZLIB_MASK;
17263e230dd2SCorentin Chary             vs->vnc_encoding = enc;
17273e230dd2SCorentin Chary             break;
1728148954faSCorentin Chary         case VNC_ENCODING_ZRLE:
1729148954faSCorentin Chary             vs->features |= VNC_FEATURE_ZRLE_MASK;
1730148954faSCorentin Chary             vs->vnc_encoding = enc;
1731148954faSCorentin Chary             break;
1732148954faSCorentin Chary         case VNC_ENCODING_ZYWRLE:
1733148954faSCorentin Chary             vs->features |= VNC_FEATURE_ZYWRLE_MASK;
1734148954faSCorentin Chary             vs->vnc_encoding = enc;
1735148954faSCorentin Chary             break;
17363e230dd2SCorentin Chary         case VNC_ENCODING_DESKTOPRESIZE:
17373e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_RESIZE_MASK;
17383e230dd2SCorentin Chary             break;
17393e230dd2SCorentin Chary         case VNC_ENCODING_POINTER_TYPE_CHANGE:
17403e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK;
17413e230dd2SCorentin Chary             break;
17423e230dd2SCorentin Chary         case VNC_ENCODING_RICH_CURSOR:
17433e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_RICH_CURSOR_MASK;
17443e230dd2SCorentin Chary             break;
17453e230dd2SCorentin Chary         case VNC_ENCODING_EXT_KEY_EVENT:
17463e230dd2SCorentin Chary             send_ext_key_event_ack(vs);
17473e230dd2SCorentin Chary             break;
17483e230dd2SCorentin Chary         case VNC_ENCODING_AUDIO:
17493e230dd2SCorentin Chary             send_ext_audio_ack(vs);
17503e230dd2SCorentin Chary             break;
17513e230dd2SCorentin Chary         case VNC_ENCODING_WMVi:
17523e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_WMVI_MASK;
17533e230dd2SCorentin Chary             break;
17543e230dd2SCorentin Chary         case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9:
1755d1af0e05SCorentin Chary             vs->tight.compression = (enc & 0x0F);
17563e230dd2SCorentin Chary             break;
17573e230dd2SCorentin Chary         case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9:
1758b31f519eSCorentin Chary             if (vs->vd->lossy) {
1759d1af0e05SCorentin Chary                 vs->tight.quality = (enc & 0x0F);
1760b31f519eSCorentin Chary             }
17613e230dd2SCorentin Chary             break;
17623e230dd2SCorentin Chary         default:
17633e230dd2SCorentin Chary             VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc);
17643e230dd2SCorentin Chary             break;
17653e230dd2SCorentin Chary         }
17663e230dd2SCorentin Chary     }
17673e230dd2SCorentin Chary     vnc_desktop_resize(vs);
17683e230dd2SCorentin Chary     check_pointer_type_change(&vs->mouse_mode_notifier);
17693e230dd2SCorentin Chary }
17703e230dd2SCorentin Chary 
17713e230dd2SCorentin Chary static void set_pixel_conversion(VncState *vs)
17723e230dd2SCorentin Chary {
17733e230dd2SCorentin Chary     if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
17743e230dd2SCorentin Chary         (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG) &&
17753e230dd2SCorentin Chary         !memcmp(&(vs->clientds.pf), &(vs->ds->surface->pf), sizeof(PixelFormat))) {
17763e230dd2SCorentin Chary         vs->write_pixels = vnc_write_pixels_copy;
17773e230dd2SCorentin Chary         vnc_hextile_set_pixel_conversion(vs, 0);
17783e230dd2SCorentin Chary     } else {
17793e230dd2SCorentin Chary         vs->write_pixels = vnc_write_pixels_generic;
17803e230dd2SCorentin Chary         vnc_hextile_set_pixel_conversion(vs, 1);
17813e230dd2SCorentin Chary     }
17823e230dd2SCorentin Chary }
17833e230dd2SCorentin Chary 
17843e230dd2SCorentin Chary static void set_pixel_format(VncState *vs,
17853e230dd2SCorentin Chary                              int bits_per_pixel, int depth,
17863e230dd2SCorentin Chary                              int big_endian_flag, int true_color_flag,
17873e230dd2SCorentin Chary                              int red_max, int green_max, int blue_max,
17883e230dd2SCorentin Chary                              int red_shift, int green_shift, int blue_shift)
17893e230dd2SCorentin Chary {
17903e230dd2SCorentin Chary     if (!true_color_flag) {
17913e230dd2SCorentin Chary         vnc_client_error(vs);
17923e230dd2SCorentin Chary         return;
17933e230dd2SCorentin Chary     }
17943e230dd2SCorentin Chary 
17953e230dd2SCorentin Chary     vs->clientds = *(vs->vd->guest.ds);
17963e230dd2SCorentin Chary     vs->clientds.pf.rmax = red_max;
1797bc2429b9SCorentin Chary     vs->clientds.pf.rbits = hweight_long(red_max);
17983e230dd2SCorentin Chary     vs->clientds.pf.rshift = red_shift;
17993e230dd2SCorentin Chary     vs->clientds.pf.rmask = red_max << red_shift;
18003e230dd2SCorentin Chary     vs->clientds.pf.gmax = green_max;
1801bc2429b9SCorentin Chary     vs->clientds.pf.gbits = hweight_long(green_max);
18023e230dd2SCorentin Chary     vs->clientds.pf.gshift = green_shift;
18033e230dd2SCorentin Chary     vs->clientds.pf.gmask = green_max << green_shift;
18043e230dd2SCorentin Chary     vs->clientds.pf.bmax = blue_max;
1805bc2429b9SCorentin Chary     vs->clientds.pf.bbits = hweight_long(blue_max);
18063e230dd2SCorentin Chary     vs->clientds.pf.bshift = blue_shift;
18073e230dd2SCorentin Chary     vs->clientds.pf.bmask = blue_max << blue_shift;
18083e230dd2SCorentin Chary     vs->clientds.pf.bits_per_pixel = bits_per_pixel;
18093e230dd2SCorentin Chary     vs->clientds.pf.bytes_per_pixel = bits_per_pixel / 8;
18103e230dd2SCorentin Chary     vs->clientds.pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel;
18113e230dd2SCorentin Chary     vs->clientds.flags = big_endian_flag ? QEMU_BIG_ENDIAN_FLAG : 0x00;
18123e230dd2SCorentin Chary 
18133e230dd2SCorentin Chary     set_pixel_conversion(vs);
18143e230dd2SCorentin Chary 
18153e230dd2SCorentin Chary     vga_hw_invalidate();
18163e230dd2SCorentin Chary     vga_hw_update();
18173e230dd2SCorentin Chary }
18183e230dd2SCorentin Chary 
18193e230dd2SCorentin Chary static void pixel_format_message (VncState *vs) {
18203e230dd2SCorentin Chary     char pad[3] = { 0, 0, 0 };
18213e230dd2SCorentin Chary 
18223e230dd2SCorentin Chary     vnc_write_u8(vs, vs->ds->surface->pf.bits_per_pixel); /* bits-per-pixel */
18233e230dd2SCorentin Chary     vnc_write_u8(vs, vs->ds->surface->pf.depth); /* depth */
18243e230dd2SCorentin Chary 
18253e230dd2SCorentin Chary #ifdef HOST_WORDS_BIGENDIAN
18263e230dd2SCorentin Chary     vnc_write_u8(vs, 1);             /* big-endian-flag */
18273e230dd2SCorentin Chary #else
18283e230dd2SCorentin Chary     vnc_write_u8(vs, 0);             /* big-endian-flag */
18293e230dd2SCorentin Chary #endif
18303e230dd2SCorentin Chary     vnc_write_u8(vs, 1);             /* true-color-flag */
18313e230dd2SCorentin Chary     vnc_write_u16(vs, vs->ds->surface->pf.rmax);     /* red-max */
18323e230dd2SCorentin Chary     vnc_write_u16(vs, vs->ds->surface->pf.gmax);     /* green-max */
18333e230dd2SCorentin Chary     vnc_write_u16(vs, vs->ds->surface->pf.bmax);     /* blue-max */
18343e230dd2SCorentin Chary     vnc_write_u8(vs, vs->ds->surface->pf.rshift);    /* red-shift */
18353e230dd2SCorentin Chary     vnc_write_u8(vs, vs->ds->surface->pf.gshift);    /* green-shift */
18363e230dd2SCorentin Chary     vnc_write_u8(vs, vs->ds->surface->pf.bshift);    /* blue-shift */
18373e230dd2SCorentin Chary 
18383e230dd2SCorentin Chary     vnc_hextile_set_pixel_conversion(vs, 0);
18393e230dd2SCorentin Chary 
18403e230dd2SCorentin Chary     vs->clientds = *(vs->ds->surface);
18413e230dd2SCorentin Chary     vs->clientds.flags &= ~QEMU_ALLOCATED_FLAG;
18423e230dd2SCorentin Chary     vs->write_pixels = vnc_write_pixels_copy;
18433e230dd2SCorentin Chary 
18443e230dd2SCorentin Chary     vnc_write(vs, pad, 3);           /* padding */
18453e230dd2SCorentin Chary }
18463e230dd2SCorentin Chary 
18473e230dd2SCorentin Chary static void vnc_dpy_setdata(DisplayState *ds)
18483e230dd2SCorentin Chary {
18493e230dd2SCorentin Chary     /* We don't have to do anything */
18503e230dd2SCorentin Chary }
18513e230dd2SCorentin Chary 
18523e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs)
18533e230dd2SCorentin Chary {
18543e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) {
18553e230dd2SCorentin Chary         /* Sending a WMVi message to notify the client*/
1856bd023f95SCorentin Chary         vnc_lock_output(vs);
18573e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
18583e230dd2SCorentin Chary         vnc_write_u8(vs, 0);
18593e230dd2SCorentin Chary         vnc_write_u16(vs, 1); /* number of rects */
18603e230dd2SCorentin Chary         vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds),
18613e230dd2SCorentin Chary                                ds_get_height(vs->ds), VNC_ENCODING_WMVi);
18623e230dd2SCorentin Chary         pixel_format_message(vs);
1863bd023f95SCorentin Chary         vnc_unlock_output(vs);
18643e230dd2SCorentin Chary         vnc_flush(vs);
18653e230dd2SCorentin Chary     } else {
18663e230dd2SCorentin Chary         set_pixel_conversion(vs);
18673e230dd2SCorentin Chary     }
18683e230dd2SCorentin Chary }
18693e230dd2SCorentin Chary 
18703e230dd2SCorentin Chary static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len)
18713e230dd2SCorentin Chary {
18723e230dd2SCorentin Chary     int i;
18733e230dd2SCorentin Chary     uint16_t limit;
18743e230dd2SCorentin Chary     VncDisplay *vd = vs->vd;
18753e230dd2SCorentin Chary 
18763e230dd2SCorentin Chary     if (data[0] > 3) {
18773e230dd2SCorentin Chary         vd->timer_interval = VNC_REFRESH_INTERVAL_BASE;
18783e230dd2SCorentin Chary         if (!qemu_timer_expired(vd->timer, qemu_get_clock(rt_clock) + vd->timer_interval))
18793e230dd2SCorentin Chary             qemu_mod_timer(vd->timer, qemu_get_clock(rt_clock) + vd->timer_interval);
18803e230dd2SCorentin Chary     }
18813e230dd2SCorentin Chary 
18823e230dd2SCorentin Chary     switch (data[0]) {
18833e230dd2SCorentin Chary     case VNC_MSG_CLIENT_SET_PIXEL_FORMAT:
18843e230dd2SCorentin Chary         if (len == 1)
18853e230dd2SCorentin Chary             return 20;
18863e230dd2SCorentin Chary 
18873e230dd2SCorentin Chary         set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5),
18883e230dd2SCorentin Chary                          read_u8(data, 6), read_u8(data, 7),
18893e230dd2SCorentin Chary                          read_u16(data, 8), read_u16(data, 10),
18903e230dd2SCorentin Chary                          read_u16(data, 12), read_u8(data, 14),
18913e230dd2SCorentin Chary                          read_u8(data, 15), read_u8(data, 16));
18923e230dd2SCorentin Chary         break;
18933e230dd2SCorentin Chary     case VNC_MSG_CLIENT_SET_ENCODINGS:
18943e230dd2SCorentin Chary         if (len == 1)
18953e230dd2SCorentin Chary             return 4;
18963e230dd2SCorentin Chary 
18973e230dd2SCorentin Chary         if (len == 4) {
18983e230dd2SCorentin Chary             limit = read_u16(data, 2);
18993e230dd2SCorentin Chary             if (limit > 0)
19003e230dd2SCorentin Chary                 return 4 + (limit * 4);
19013e230dd2SCorentin Chary         } else
19023e230dd2SCorentin Chary             limit = read_u16(data, 2);
19033e230dd2SCorentin Chary 
19043e230dd2SCorentin Chary         for (i = 0; i < limit; i++) {
19053e230dd2SCorentin Chary             int32_t val = read_s32(data, 4 + (i * 4));
19063e230dd2SCorentin Chary             memcpy(data + 4 + (i * 4), &val, sizeof(val));
19073e230dd2SCorentin Chary         }
19083e230dd2SCorentin Chary 
19093e230dd2SCorentin Chary         set_encodings(vs, (int32_t *)(data + 4), limit);
19103e230dd2SCorentin Chary         break;
19113e230dd2SCorentin Chary     case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST:
19123e230dd2SCorentin Chary         if (len == 1)
19133e230dd2SCorentin Chary             return 10;
19143e230dd2SCorentin Chary 
19153e230dd2SCorentin Chary         framebuffer_update_request(vs,
19163e230dd2SCorentin Chary                                    read_u8(data, 1), read_u16(data, 2), read_u16(data, 4),
19173e230dd2SCorentin Chary                                    read_u16(data, 6), read_u16(data, 8));
19183e230dd2SCorentin Chary         break;
19193e230dd2SCorentin Chary     case VNC_MSG_CLIENT_KEY_EVENT:
19203e230dd2SCorentin Chary         if (len == 1)
19213e230dd2SCorentin Chary             return 8;
19223e230dd2SCorentin Chary 
19233e230dd2SCorentin Chary         key_event(vs, read_u8(data, 1), read_u32(data, 4));
19243e230dd2SCorentin Chary         break;
19253e230dd2SCorentin Chary     case VNC_MSG_CLIENT_POINTER_EVENT:
19263e230dd2SCorentin Chary         if (len == 1)
19273e230dd2SCorentin Chary             return 6;
19283e230dd2SCorentin Chary 
19293e230dd2SCorentin Chary         pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
19303e230dd2SCorentin Chary         break;
19313e230dd2SCorentin Chary     case VNC_MSG_CLIENT_CUT_TEXT:
19323e230dd2SCorentin Chary         if (len == 1)
19333e230dd2SCorentin Chary             return 8;
19343e230dd2SCorentin Chary 
19353e230dd2SCorentin Chary         if (len == 8) {
19363e230dd2SCorentin Chary             uint32_t dlen = read_u32(data, 4);
19373e230dd2SCorentin Chary             if (dlen > 0)
19383e230dd2SCorentin Chary                 return 8 + dlen;
19393e230dd2SCorentin Chary         }
19403e230dd2SCorentin Chary 
19413e230dd2SCorentin Chary         client_cut_text(vs, read_u32(data, 4), data + 8);
19423e230dd2SCorentin Chary         break;
19433e230dd2SCorentin Chary     case VNC_MSG_CLIENT_QEMU:
19443e230dd2SCorentin Chary         if (len == 1)
19453e230dd2SCorentin Chary             return 2;
19463e230dd2SCorentin Chary 
19473e230dd2SCorentin Chary         switch (read_u8(data, 1)) {
19483e230dd2SCorentin Chary         case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT:
19493e230dd2SCorentin Chary             if (len == 2)
19503e230dd2SCorentin Chary                 return 12;
19513e230dd2SCorentin Chary 
19523e230dd2SCorentin Chary             ext_key_event(vs, read_u16(data, 2),
19533e230dd2SCorentin Chary                           read_u32(data, 4), read_u32(data, 8));
19543e230dd2SCorentin Chary             break;
19553e230dd2SCorentin Chary         case VNC_MSG_CLIENT_QEMU_AUDIO:
19563e230dd2SCorentin Chary             if (len == 2)
19573e230dd2SCorentin Chary                 return 4;
19583e230dd2SCorentin Chary 
19593e230dd2SCorentin Chary             switch (read_u16 (data, 2)) {
19603e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE:
19613e230dd2SCorentin Chary                 audio_add(vs);
19623e230dd2SCorentin Chary                 break;
19633e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE:
19643e230dd2SCorentin Chary                 audio_del(vs);
19653e230dd2SCorentin Chary                 break;
19663e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT:
19673e230dd2SCorentin Chary                 if (len == 4)
19683e230dd2SCorentin Chary                     return 10;
19693e230dd2SCorentin Chary                 switch (read_u8(data, 4)) {
19703e230dd2SCorentin Chary                 case 0: vs->as.fmt = AUD_FMT_U8; break;
19713e230dd2SCorentin Chary                 case 1: vs->as.fmt = AUD_FMT_S8; break;
19723e230dd2SCorentin Chary                 case 2: vs->as.fmt = AUD_FMT_U16; break;
19733e230dd2SCorentin Chary                 case 3: vs->as.fmt = AUD_FMT_S16; break;
19743e230dd2SCorentin Chary                 case 4: vs->as.fmt = AUD_FMT_U32; break;
19753e230dd2SCorentin Chary                 case 5: vs->as.fmt = AUD_FMT_S32; break;
19763e230dd2SCorentin Chary                 default:
19773e230dd2SCorentin Chary                     printf("Invalid audio format %d\n", read_u8(data, 4));
19783e230dd2SCorentin Chary                     vnc_client_error(vs);
19793e230dd2SCorentin Chary                     break;
19803e230dd2SCorentin Chary                 }
19813e230dd2SCorentin Chary                 vs->as.nchannels = read_u8(data, 5);
19823e230dd2SCorentin Chary                 if (vs->as.nchannels != 1 && vs->as.nchannels != 2) {
19833e230dd2SCorentin Chary                     printf("Invalid audio channel coount %d\n",
19843e230dd2SCorentin Chary                            read_u8(data, 5));
19853e230dd2SCorentin Chary                     vnc_client_error(vs);
19863e230dd2SCorentin Chary                     break;
19873e230dd2SCorentin Chary                 }
19883e230dd2SCorentin Chary                 vs->as.freq = read_u32(data, 6);
19893e230dd2SCorentin Chary                 break;
19903e230dd2SCorentin Chary             default:
19913e230dd2SCorentin Chary                 printf ("Invalid audio message %d\n", read_u8(data, 4));
19923e230dd2SCorentin Chary                 vnc_client_error(vs);
19933e230dd2SCorentin Chary                 break;
19943e230dd2SCorentin Chary             }
19953e230dd2SCorentin Chary             break;
19963e230dd2SCorentin Chary 
19973e230dd2SCorentin Chary         default:
19983e230dd2SCorentin Chary             printf("Msg: %d\n", read_u16(data, 0));
19993e230dd2SCorentin Chary             vnc_client_error(vs);
20003e230dd2SCorentin Chary             break;
20013e230dd2SCorentin Chary         }
20023e230dd2SCorentin Chary         break;
20033e230dd2SCorentin Chary     default:
20043e230dd2SCorentin Chary         printf("Msg: %d\n", data[0]);
20053e230dd2SCorentin Chary         vnc_client_error(vs);
20063e230dd2SCorentin Chary         break;
20073e230dd2SCorentin Chary     }
20083e230dd2SCorentin Chary 
20093e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_msg, 1);
20103e230dd2SCorentin Chary     return 0;
20113e230dd2SCorentin Chary }
20123e230dd2SCorentin Chary 
20133e230dd2SCorentin Chary static int protocol_client_init(VncState *vs, uint8_t *data, size_t len)
20143e230dd2SCorentin Chary {
20153e230dd2SCorentin Chary     char buf[1024];
20163e230dd2SCorentin Chary     int size;
20173e230dd2SCorentin Chary 
20183e230dd2SCorentin Chary     vs->client_width = ds_get_width(vs->ds);
20193e230dd2SCorentin Chary     vs->client_height = ds_get_height(vs->ds);
20203e230dd2SCorentin Chary     vnc_write_u16(vs, vs->client_width);
20213e230dd2SCorentin Chary     vnc_write_u16(vs, vs->client_height);
20223e230dd2SCorentin Chary 
20233e230dd2SCorentin Chary     pixel_format_message(vs);
20243e230dd2SCorentin Chary 
20253e230dd2SCorentin Chary     if (qemu_name)
20263e230dd2SCorentin Chary         size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name);
20273e230dd2SCorentin Chary     else
20283e230dd2SCorentin Chary         size = snprintf(buf, sizeof(buf), "QEMU");
20293e230dd2SCorentin Chary 
20303e230dd2SCorentin Chary     vnc_write_u32(vs, size);
20313e230dd2SCorentin Chary     vnc_write(vs, buf, size);
20323e230dd2SCorentin Chary     vnc_flush(vs);
20333e230dd2SCorentin Chary 
20343e230dd2SCorentin Chary     vnc_client_cache_auth(vs);
20353e230dd2SCorentin Chary     vnc_qmp_event(vs, QEVENT_VNC_INITIALIZED);
20363e230dd2SCorentin Chary 
20373e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_msg, 1);
20383e230dd2SCorentin Chary 
20393e230dd2SCorentin Chary     return 0;
20403e230dd2SCorentin Chary }
20413e230dd2SCorentin Chary 
20423e230dd2SCorentin Chary void start_client_init(VncState *vs)
20433e230dd2SCorentin Chary {
20443e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_init, 1);
20453e230dd2SCorentin Chary }
20463e230dd2SCorentin Chary 
20473e230dd2SCorentin Chary static void make_challenge(VncState *vs)
20483e230dd2SCorentin Chary {
20493e230dd2SCorentin Chary     int i;
20503e230dd2SCorentin Chary 
20513e230dd2SCorentin Chary     srand(time(NULL)+getpid()+getpid()*987654+rand());
20523e230dd2SCorentin Chary 
20533e230dd2SCorentin Chary     for (i = 0 ; i < sizeof(vs->challenge) ; i++)
20543e230dd2SCorentin Chary         vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0));
20553e230dd2SCorentin Chary }
20563e230dd2SCorentin Chary 
20573e230dd2SCorentin Chary static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len)
20583e230dd2SCorentin Chary {
20593e230dd2SCorentin Chary     unsigned char response[VNC_AUTH_CHALLENGE_SIZE];
20603e230dd2SCorentin Chary     int i, j, pwlen;
20613e230dd2SCorentin Chary     unsigned char key[8];
20623c9405a0SGerd Hoffmann     time_t now = time(NULL);
20633e230dd2SCorentin Chary 
20641cd20f8bSAnthony Liguori     if (!vs->vd->password) {
20653e230dd2SCorentin Chary         VNC_DEBUG("No password configured on server");
20666bffdf0fSGerd Hoffmann         goto reject;
20673e230dd2SCorentin Chary     }
20683c9405a0SGerd Hoffmann     if (vs->vd->expires < now) {
20693c9405a0SGerd Hoffmann         VNC_DEBUG("Password is expired");
20703c9405a0SGerd Hoffmann         goto reject;
20713c9405a0SGerd Hoffmann     }
20723e230dd2SCorentin Chary 
20733e230dd2SCorentin Chary     memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE);
20743e230dd2SCorentin Chary 
20753e230dd2SCorentin Chary     /* Calculate the expected challenge response */
20763e230dd2SCorentin Chary     pwlen = strlen(vs->vd->password);
20773e230dd2SCorentin Chary     for (i=0; i<sizeof(key); i++)
20783e230dd2SCorentin Chary         key[i] = i<pwlen ? vs->vd->password[i] : 0;
20793e230dd2SCorentin Chary     deskey(key, EN0);
20803e230dd2SCorentin Chary     for (j = 0; j < VNC_AUTH_CHALLENGE_SIZE; j += 8)
20813e230dd2SCorentin Chary         des(response+j, response+j);
20823e230dd2SCorentin Chary 
20833e230dd2SCorentin Chary     /* Compare expected vs actual challenge response */
20843e230dd2SCorentin Chary     if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) {
20853e230dd2SCorentin Chary         VNC_DEBUG("Client challenge reponse did not match\n");
20866bffdf0fSGerd Hoffmann         goto reject;
20876bffdf0fSGerd Hoffmann     } else {
20886bffdf0fSGerd Hoffmann         VNC_DEBUG("Accepting VNC challenge response\n");
20896bffdf0fSGerd Hoffmann         vnc_write_u32(vs, 0); /* Accept auth */
20906bffdf0fSGerd Hoffmann         vnc_flush(vs);
20916bffdf0fSGerd Hoffmann 
20926bffdf0fSGerd Hoffmann         start_client_init(vs);
20936bffdf0fSGerd Hoffmann     }
20946bffdf0fSGerd Hoffmann     return 0;
20956bffdf0fSGerd Hoffmann 
20966bffdf0fSGerd Hoffmann reject:
20973e230dd2SCorentin Chary     vnc_write_u32(vs, 1); /* Reject auth */
20983e230dd2SCorentin Chary     if (vs->minor >= 8) {
20993e230dd2SCorentin Chary         static const char err[] = "Authentication failed";
21003e230dd2SCorentin Chary         vnc_write_u32(vs, sizeof(err));
21013e230dd2SCorentin Chary         vnc_write(vs, err, sizeof(err));
21023e230dd2SCorentin Chary     }
21033e230dd2SCorentin Chary     vnc_flush(vs);
21043e230dd2SCorentin Chary     vnc_client_error(vs);
21053e230dd2SCorentin Chary     return 0;
21063e230dd2SCorentin Chary }
21073e230dd2SCorentin Chary 
21083e230dd2SCorentin Chary void start_auth_vnc(VncState *vs)
21093e230dd2SCorentin Chary {
21103e230dd2SCorentin Chary     make_challenge(vs);
21113e230dd2SCorentin Chary     /* Send client a 'random' challenge */
21123e230dd2SCorentin Chary     vnc_write(vs, vs->challenge, sizeof(vs->challenge));
21133e230dd2SCorentin Chary     vnc_flush(vs);
21143e230dd2SCorentin Chary 
21153e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge));
21163e230dd2SCorentin Chary }
21173e230dd2SCorentin Chary 
21183e230dd2SCorentin Chary 
21193e230dd2SCorentin Chary static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len)
21203e230dd2SCorentin Chary {
21213e230dd2SCorentin Chary     /* We only advertise 1 auth scheme at a time, so client
21223e230dd2SCorentin Chary      * must pick the one we sent. Verify this */
21233e230dd2SCorentin Chary     if (data[0] != vs->vd->auth) { /* Reject auth */
21243e230dd2SCorentin Chary        VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data[0]);
21253e230dd2SCorentin Chary        vnc_write_u32(vs, 1);
21263e230dd2SCorentin Chary        if (vs->minor >= 8) {
21273e230dd2SCorentin Chary            static const char err[] = "Authentication failed";
21283e230dd2SCorentin Chary            vnc_write_u32(vs, sizeof(err));
21293e230dd2SCorentin Chary            vnc_write(vs, err, sizeof(err));
21303e230dd2SCorentin Chary        }
21313e230dd2SCorentin Chary        vnc_client_error(vs);
21323e230dd2SCorentin Chary     } else { /* Accept requested auth */
21333e230dd2SCorentin Chary        VNC_DEBUG("Client requested auth %d\n", (int)data[0]);
21343e230dd2SCorentin Chary        switch (vs->vd->auth) {
21353e230dd2SCorentin Chary        case VNC_AUTH_NONE:
21363e230dd2SCorentin Chary            VNC_DEBUG("Accept auth none\n");
21373e230dd2SCorentin Chary            if (vs->minor >= 8) {
21383e230dd2SCorentin Chary                vnc_write_u32(vs, 0); /* Accept auth completion */
21393e230dd2SCorentin Chary                vnc_flush(vs);
21403e230dd2SCorentin Chary            }
21413e230dd2SCorentin Chary            start_client_init(vs);
21423e230dd2SCorentin Chary            break;
21433e230dd2SCorentin Chary 
21443e230dd2SCorentin Chary        case VNC_AUTH_VNC:
21453e230dd2SCorentin Chary            VNC_DEBUG("Start VNC auth\n");
21463e230dd2SCorentin Chary            start_auth_vnc(vs);
21473e230dd2SCorentin Chary            break;
21483e230dd2SCorentin Chary 
21493e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
21503e230dd2SCorentin Chary        case VNC_AUTH_VENCRYPT:
21513e230dd2SCorentin Chary            VNC_DEBUG("Accept VeNCrypt auth\n");;
21523e230dd2SCorentin Chary            start_auth_vencrypt(vs);
21533e230dd2SCorentin Chary            break;
21543e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
21553e230dd2SCorentin Chary 
21563e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
21573e230dd2SCorentin Chary        case VNC_AUTH_SASL:
21583e230dd2SCorentin Chary            VNC_DEBUG("Accept SASL auth\n");
21593e230dd2SCorentin Chary            start_auth_sasl(vs);
21603e230dd2SCorentin Chary            break;
21613e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
21623e230dd2SCorentin Chary 
21633e230dd2SCorentin Chary        default: /* Should not be possible, but just in case */
21643e230dd2SCorentin Chary            VNC_DEBUG("Reject auth %d server code bug\n", vs->vd->auth);
21653e230dd2SCorentin Chary            vnc_write_u8(vs, 1);
21663e230dd2SCorentin Chary            if (vs->minor >= 8) {
21673e230dd2SCorentin Chary                static const char err[] = "Authentication failed";
21683e230dd2SCorentin Chary                vnc_write_u32(vs, sizeof(err));
21693e230dd2SCorentin Chary                vnc_write(vs, err, sizeof(err));
21703e230dd2SCorentin Chary            }
21713e230dd2SCorentin Chary            vnc_client_error(vs);
21723e230dd2SCorentin Chary        }
21733e230dd2SCorentin Chary     }
21743e230dd2SCorentin Chary     return 0;
21753e230dd2SCorentin Chary }
21763e230dd2SCorentin Chary 
21773e230dd2SCorentin Chary static int protocol_version(VncState *vs, uint8_t *version, size_t len)
21783e230dd2SCorentin Chary {
21793e230dd2SCorentin Chary     char local[13];
21803e230dd2SCorentin Chary 
21813e230dd2SCorentin Chary     memcpy(local, version, 12);
21823e230dd2SCorentin Chary     local[12] = 0;
21833e230dd2SCorentin Chary 
21843e230dd2SCorentin Chary     if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) {
21853e230dd2SCorentin Chary         VNC_DEBUG("Malformed protocol version %s\n", local);
21863e230dd2SCorentin Chary         vnc_client_error(vs);
21873e230dd2SCorentin Chary         return 0;
21883e230dd2SCorentin Chary     }
21893e230dd2SCorentin Chary     VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor);
21903e230dd2SCorentin Chary     if (vs->major != 3 ||
21913e230dd2SCorentin Chary         (vs->minor != 3 &&
21923e230dd2SCorentin Chary          vs->minor != 4 &&
21933e230dd2SCorentin Chary          vs->minor != 5 &&
21943e230dd2SCorentin Chary          vs->minor != 7 &&
21953e230dd2SCorentin Chary          vs->minor != 8)) {
21963e230dd2SCorentin Chary         VNC_DEBUG("Unsupported client version\n");
21973e230dd2SCorentin Chary         vnc_write_u32(vs, VNC_AUTH_INVALID);
21983e230dd2SCorentin Chary         vnc_flush(vs);
21993e230dd2SCorentin Chary         vnc_client_error(vs);
22003e230dd2SCorentin Chary         return 0;
22013e230dd2SCorentin Chary     }
22023e230dd2SCorentin Chary     /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
22033e230dd2SCorentin Chary      * as equivalent to v3.3 by servers
22043e230dd2SCorentin Chary      */
22053e230dd2SCorentin Chary     if (vs->minor == 4 || vs->minor == 5)
22063e230dd2SCorentin Chary         vs->minor = 3;
22073e230dd2SCorentin Chary 
22083e230dd2SCorentin Chary     if (vs->minor == 3) {
22093e230dd2SCorentin Chary         if (vs->vd->auth == VNC_AUTH_NONE) {
22103e230dd2SCorentin Chary             VNC_DEBUG("Tell client auth none\n");
22113e230dd2SCorentin Chary             vnc_write_u32(vs, vs->vd->auth);
22123e230dd2SCorentin Chary             vnc_flush(vs);
22133e230dd2SCorentin Chary             start_client_init(vs);
22143e230dd2SCorentin Chary        } else if (vs->vd->auth == VNC_AUTH_VNC) {
22153e230dd2SCorentin Chary             VNC_DEBUG("Tell client VNC auth\n");
22163e230dd2SCorentin Chary             vnc_write_u32(vs, vs->vd->auth);
22173e230dd2SCorentin Chary             vnc_flush(vs);
22183e230dd2SCorentin Chary             start_auth_vnc(vs);
22193e230dd2SCorentin Chary        } else {
22203e230dd2SCorentin Chary             VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->vd->auth);
22213e230dd2SCorentin Chary             vnc_write_u32(vs, VNC_AUTH_INVALID);
22223e230dd2SCorentin Chary             vnc_flush(vs);
22233e230dd2SCorentin Chary             vnc_client_error(vs);
22243e230dd2SCorentin Chary        }
22253e230dd2SCorentin Chary     } else {
22263e230dd2SCorentin Chary         VNC_DEBUG("Telling client we support auth %d\n", vs->vd->auth);
22273e230dd2SCorentin Chary         vnc_write_u8(vs, 1); /* num auth */
22283e230dd2SCorentin Chary         vnc_write_u8(vs, vs->vd->auth);
22293e230dd2SCorentin Chary         vnc_read_when(vs, protocol_client_auth, 1);
22303e230dd2SCorentin Chary         vnc_flush(vs);
22313e230dd2SCorentin Chary     }
22323e230dd2SCorentin Chary 
22333e230dd2SCorentin Chary     return 0;
22343e230dd2SCorentin Chary }
22353e230dd2SCorentin Chary 
2236999342a0SCorentin Chary static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y)
2237999342a0SCorentin Chary {
2238999342a0SCorentin Chary     struct VncSurface *vs = &vd->guest;
2239999342a0SCorentin Chary 
2240999342a0SCorentin Chary     return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT];
2241999342a0SCorentin Chary }
2242999342a0SCorentin Chary 
22437d964c9dSCorentin Chary void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h)
22447d964c9dSCorentin Chary {
22457d964c9dSCorentin Chary     int i, j;
22467d964c9dSCorentin Chary 
22477d964c9dSCorentin Chary     w = (x + w) / VNC_STAT_RECT;
22487d964c9dSCorentin Chary     h = (y + h) / VNC_STAT_RECT;
22497d964c9dSCorentin Chary     x /= VNC_STAT_RECT;
22507d964c9dSCorentin Chary     y /= VNC_STAT_RECT;
22517d964c9dSCorentin Chary 
2252207f328aSCorentin Chary     for (j = y; j <= h; j++) {
2253207f328aSCorentin Chary         for (i = x; i <= w; i++) {
22547d964c9dSCorentin Chary             vs->lossy_rect[j][i] = 1;
22557d964c9dSCorentin Chary         }
22567d964c9dSCorentin Chary     }
22577d964c9dSCorentin Chary }
22587d964c9dSCorentin Chary 
22597d964c9dSCorentin Chary static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y)
22607d964c9dSCorentin Chary {
22617d964c9dSCorentin Chary     VncState *vs;
22627d964c9dSCorentin Chary     int sty = y / VNC_STAT_RECT;
22637d964c9dSCorentin Chary     int stx = x / VNC_STAT_RECT;
22647d964c9dSCorentin Chary     int has_dirty = 0;
22657d964c9dSCorentin Chary 
22667d964c9dSCorentin Chary     y = y / VNC_STAT_RECT * VNC_STAT_RECT;
22677d964c9dSCorentin Chary     x = x / VNC_STAT_RECT * VNC_STAT_RECT;
22687d964c9dSCorentin Chary 
22697d964c9dSCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
2270bc2429b9SCorentin Chary         int j;
22717d964c9dSCorentin Chary 
22727d964c9dSCorentin Chary         /* kernel send buffers are full -> refresh later */
22737d964c9dSCorentin Chary         if (vs->output.offset) {
22747d964c9dSCorentin Chary             continue;
22757d964c9dSCorentin Chary         }
22767d964c9dSCorentin Chary 
22777d964c9dSCorentin Chary         if (!vs->lossy_rect[sty][stx]) {
22787d964c9dSCorentin Chary             continue;
22797d964c9dSCorentin Chary         }
2280207f328aSCorentin Chary 
22817d964c9dSCorentin Chary         vs->lossy_rect[sty][stx] = 0;
22827d964c9dSCorentin Chary         for (j = 0; j < VNC_STAT_RECT; ++j) {
2283bc2429b9SCorentin Chary             bitmap_set(vs->dirty[y + j], x / 16, VNC_STAT_RECT / 16);
22847d964c9dSCorentin Chary         }
22857d964c9dSCorentin Chary         has_dirty++;
22867d964c9dSCorentin Chary     }
2287207f328aSCorentin Chary 
22887d964c9dSCorentin Chary     return has_dirty;
22897d964c9dSCorentin Chary }
22907d964c9dSCorentin Chary 
22917d964c9dSCorentin Chary static int vnc_update_stats(VncDisplay *vd,  struct timeval * tv)
2292999342a0SCorentin Chary {
2293999342a0SCorentin Chary     int x, y;
2294999342a0SCorentin Chary     struct timeval res;
22957d964c9dSCorentin Chary     int has_dirty = 0;
2296999342a0SCorentin Chary 
2297999342a0SCorentin Chary     for (y = 0; y < vd->guest.ds->height; y += VNC_STAT_RECT) {
2298999342a0SCorentin Chary         for (x = 0; x < vd->guest.ds->width; x += VNC_STAT_RECT) {
2299999342a0SCorentin Chary             VncRectStat *rect = vnc_stat_rect(vd, x, y);
2300999342a0SCorentin Chary 
2301999342a0SCorentin Chary             rect->updated = false;
2302999342a0SCorentin Chary         }
2303999342a0SCorentin Chary     }
2304999342a0SCorentin Chary 
2305999342a0SCorentin Chary     timersub(tv, &VNC_REFRESH_STATS, &res);
2306999342a0SCorentin Chary 
2307999342a0SCorentin Chary     if (timercmp(&vd->guest.last_freq_check, &res, >)) {
23087d964c9dSCorentin Chary         return has_dirty;
2309999342a0SCorentin Chary     }
2310999342a0SCorentin Chary     vd->guest.last_freq_check = *tv;
2311999342a0SCorentin Chary 
2312999342a0SCorentin Chary     for (y = 0; y < vd->guest.ds->height; y += VNC_STAT_RECT) {
2313999342a0SCorentin Chary         for (x = 0; x < vd->guest.ds->width; x += VNC_STAT_RECT) {
2314999342a0SCorentin Chary             VncRectStat *rect= vnc_stat_rect(vd, x, y);
2315999342a0SCorentin Chary             int count = ARRAY_SIZE(rect->times);
2316999342a0SCorentin Chary             struct timeval min, max;
2317999342a0SCorentin Chary 
2318999342a0SCorentin Chary             if (!timerisset(&rect->times[count - 1])) {
2319999342a0SCorentin Chary                 continue ;
2320999342a0SCorentin Chary             }
2321999342a0SCorentin Chary 
2322999342a0SCorentin Chary             max = rect->times[(rect->idx + count - 1) % count];
2323999342a0SCorentin Chary             timersub(tv, &max, &res);
2324999342a0SCorentin Chary 
2325999342a0SCorentin Chary             if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) {
2326999342a0SCorentin Chary                 rect->freq = 0;
23277d964c9dSCorentin Chary                 has_dirty += vnc_refresh_lossy_rect(vd, x, y);
2328999342a0SCorentin Chary                 memset(rect->times, 0, sizeof (rect->times));
2329999342a0SCorentin Chary                 continue ;
2330999342a0SCorentin Chary             }
2331999342a0SCorentin Chary 
2332999342a0SCorentin Chary             min = rect->times[rect->idx];
2333999342a0SCorentin Chary             max = rect->times[(rect->idx + count - 1) % count];
2334999342a0SCorentin Chary             timersub(&max, &min, &res);
2335999342a0SCorentin Chary 
2336999342a0SCorentin Chary             rect->freq = res.tv_sec + res.tv_usec / 1000000.;
2337999342a0SCorentin Chary             rect->freq /= count;
2338999342a0SCorentin Chary             rect->freq = 1. / rect->freq;
2339999342a0SCorentin Chary         }
2340999342a0SCorentin Chary     }
23417d964c9dSCorentin Chary     return has_dirty;
2342999342a0SCorentin Chary }
2343999342a0SCorentin Chary 
2344999342a0SCorentin Chary double vnc_update_freq(VncState *vs, int x, int y, int w, int h)
2345999342a0SCorentin Chary {
2346999342a0SCorentin Chary     int i, j;
2347999342a0SCorentin Chary     double total = 0;
2348999342a0SCorentin Chary     int num = 0;
2349999342a0SCorentin Chary 
2350999342a0SCorentin Chary     x =  (x / VNC_STAT_RECT) * VNC_STAT_RECT;
2351999342a0SCorentin Chary     y =  (y / VNC_STAT_RECT) * VNC_STAT_RECT;
2352999342a0SCorentin Chary 
2353999342a0SCorentin Chary     for (j = y; j <= y + h; j += VNC_STAT_RECT) {
2354999342a0SCorentin Chary         for (i = x; i <= x + w; i += VNC_STAT_RECT) {
2355999342a0SCorentin Chary             total += vnc_stat_rect(vs->vd, i, j)->freq;
2356999342a0SCorentin Chary             num++;
2357999342a0SCorentin Chary         }
2358999342a0SCorentin Chary     }
2359999342a0SCorentin Chary 
2360999342a0SCorentin Chary     if (num) {
2361999342a0SCorentin Chary         return total / num;
2362999342a0SCorentin Chary     } else {
2363999342a0SCorentin Chary         return 0;
2364999342a0SCorentin Chary     }
2365999342a0SCorentin Chary }
2366999342a0SCorentin Chary 
2367999342a0SCorentin Chary static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv)
2368999342a0SCorentin Chary {
2369999342a0SCorentin Chary     VncRectStat *rect;
2370999342a0SCorentin Chary 
2371999342a0SCorentin Chary     rect = vnc_stat_rect(vd, x, y);
2372999342a0SCorentin Chary     if (rect->updated) {
2373999342a0SCorentin Chary         return ;
2374999342a0SCorentin Chary     }
2375999342a0SCorentin Chary     rect->times[rect->idx] = *tv;
2376999342a0SCorentin Chary     rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times);
2377999342a0SCorentin Chary     rect->updated = true;
2378999342a0SCorentin Chary }
2379999342a0SCorentin Chary 
23803e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd)
23813e230dd2SCorentin Chary {
23823e230dd2SCorentin Chary     int y;
23833e230dd2SCorentin Chary     uint8_t *guest_row;
23843e230dd2SCorentin Chary     uint8_t *server_row;
23853e230dd2SCorentin Chary     int cmp_bytes;
2386bc2429b9SCorentin Chary     unsigned long width_mask[VNC_DIRTY_WORDS];
23873e230dd2SCorentin Chary     VncState *vs;
23883e230dd2SCorentin Chary     int has_dirty = 0;
23893e230dd2SCorentin Chary 
2390999342a0SCorentin Chary     struct timeval tv;
2391999342a0SCorentin Chary 
2392999342a0SCorentin Chary     gettimeofday(&tv, NULL);
23937d964c9dSCorentin Chary     has_dirty = vnc_update_stats(vd, &tv);
2394999342a0SCorentin Chary 
23953e230dd2SCorentin Chary     /*
23963e230dd2SCorentin Chary      * Walk through the guest dirty map.
23973e230dd2SCorentin Chary      * Check and copy modified bits from guest to server surface.
23983e230dd2SCorentin Chary      * Update server dirty map.
23993e230dd2SCorentin Chary      */
2400bc2429b9SCorentin Chary     bitmap_set(width_mask, 0, (ds_get_width(vd->ds) / 16));
2401bc2429b9SCorentin Chary     bitmap_clear(width_mask, (ds_get_width(vd->ds) / 16),
2402bc2429b9SCorentin Chary                  VNC_DIRTY_WORDS * BITS_PER_LONG);
24033e230dd2SCorentin Chary     cmp_bytes = 16 * ds_get_bytes_per_pixel(vd->ds);
24043e230dd2SCorentin Chary     guest_row  = vd->guest.ds->data;
24053e230dd2SCorentin Chary     server_row = vd->server->data;
24063e230dd2SCorentin Chary     for (y = 0; y < vd->guest.ds->height; y++) {
2407bc2429b9SCorentin Chary         if (bitmap_intersects(vd->guest.dirty[y], width_mask, VNC_DIRTY_WORDS)) {
24083e230dd2SCorentin Chary             int x;
24093e230dd2SCorentin Chary             uint8_t *guest_ptr;
24103e230dd2SCorentin Chary             uint8_t *server_ptr;
24113e230dd2SCorentin Chary 
24123e230dd2SCorentin Chary             guest_ptr  = guest_row;
24133e230dd2SCorentin Chary             server_ptr = server_row;
24143e230dd2SCorentin Chary 
24153e230dd2SCorentin Chary             for (x = 0; x < vd->guest.ds->width;
24163e230dd2SCorentin Chary                     x += 16, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) {
2417bc2429b9SCorentin Chary                 if (!test_and_clear_bit((x / 16), vd->guest.dirty[y]))
24183e230dd2SCorentin Chary                     continue;
24193e230dd2SCorentin Chary                 if (memcmp(server_ptr, guest_ptr, cmp_bytes) == 0)
24203e230dd2SCorentin Chary                     continue;
24213e230dd2SCorentin Chary                 memcpy(server_ptr, guest_ptr, cmp_bytes);
2422999342a0SCorentin Chary                 vnc_rect_updated(vd, x, y, &tv);
24233e230dd2SCorentin Chary                 QTAILQ_FOREACH(vs, &vd->clients, next) {
2424bc2429b9SCorentin Chary                     set_bit((x / 16), vs->dirty[y]);
24253e230dd2SCorentin Chary                 }
24263e230dd2SCorentin Chary                 has_dirty++;
24273e230dd2SCorentin Chary             }
24283e230dd2SCorentin Chary         }
24293e230dd2SCorentin Chary         guest_row  += ds_get_linesize(vd->ds);
24303e230dd2SCorentin Chary         server_row += ds_get_linesize(vd->ds);
24313e230dd2SCorentin Chary     }
24323e230dd2SCorentin Chary     return has_dirty;
24333e230dd2SCorentin Chary }
24343e230dd2SCorentin Chary 
24353e230dd2SCorentin Chary static void vnc_refresh(void *opaque)
24363e230dd2SCorentin Chary {
24373e230dd2SCorentin Chary     VncDisplay *vd = opaque;
24383e230dd2SCorentin Chary     VncState *vs, *vn;
24393e230dd2SCorentin Chary     int has_dirty, rects = 0;
24403e230dd2SCorentin Chary 
24413e230dd2SCorentin Chary     vga_hw_update();
24423e230dd2SCorentin Chary 
2443bd023f95SCorentin Chary     if (vnc_trylock_display(vd)) {
2444bd023f95SCorentin Chary         vd->timer_interval = VNC_REFRESH_INTERVAL_BASE;
2445bd023f95SCorentin Chary         qemu_mod_timer(vd->timer, qemu_get_clock(rt_clock) +
2446bd023f95SCorentin Chary                        vd->timer_interval);
2447bd023f95SCorentin Chary         return;
2448bd023f95SCorentin Chary     }
2449bd023f95SCorentin Chary 
24503e230dd2SCorentin Chary     has_dirty = vnc_refresh_server_surface(vd);
2451bd023f95SCorentin Chary     vnc_unlock_display(vd);
24523e230dd2SCorentin Chary 
24533e230dd2SCorentin Chary     QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
24543e230dd2SCorentin Chary         rects += vnc_update_client(vs, has_dirty);
24553e230dd2SCorentin Chary         /* vs might be free()ed here */
24563e230dd2SCorentin Chary     }
2457bd023f95SCorentin Chary 
24583e230dd2SCorentin Chary     /* vd->timer could be NULL now if the last client disconnected,
24593e230dd2SCorentin Chary      * in this case don't update the timer */
24603e230dd2SCorentin Chary     if (vd->timer == NULL)
24613e230dd2SCorentin Chary         return;
24623e230dd2SCorentin Chary 
24633e230dd2SCorentin Chary     if (has_dirty && rects) {
24643e230dd2SCorentin Chary         vd->timer_interval /= 2;
24653e230dd2SCorentin Chary         if (vd->timer_interval < VNC_REFRESH_INTERVAL_BASE)
24663e230dd2SCorentin Chary             vd->timer_interval = VNC_REFRESH_INTERVAL_BASE;
24673e230dd2SCorentin Chary     } else {
24683e230dd2SCorentin Chary         vd->timer_interval += VNC_REFRESH_INTERVAL_INC;
24693e230dd2SCorentin Chary         if (vd->timer_interval > VNC_REFRESH_INTERVAL_MAX)
24703e230dd2SCorentin Chary             vd->timer_interval = VNC_REFRESH_INTERVAL_MAX;
24713e230dd2SCorentin Chary     }
24723e230dd2SCorentin Chary     qemu_mod_timer(vd->timer, qemu_get_clock(rt_clock) + vd->timer_interval);
24733e230dd2SCorentin Chary }
24743e230dd2SCorentin Chary 
24753e230dd2SCorentin Chary static void vnc_init_timer(VncDisplay *vd)
24763e230dd2SCorentin Chary {
24773e230dd2SCorentin Chary     vd->timer_interval = VNC_REFRESH_INTERVAL_BASE;
24783e230dd2SCorentin Chary     if (vd->timer == NULL && !QTAILQ_EMPTY(&vd->clients)) {
24793e230dd2SCorentin Chary         vd->timer = qemu_new_timer(rt_clock, vnc_refresh, vd);
24803e230dd2SCorentin Chary         vnc_refresh(vd);
24813e230dd2SCorentin Chary     }
24823e230dd2SCorentin Chary }
24833e230dd2SCorentin Chary 
24843e230dd2SCorentin Chary static void vnc_remove_timer(VncDisplay *vd)
24853e230dd2SCorentin Chary {
24863e230dd2SCorentin Chary     if (vd->timer != NULL && QTAILQ_EMPTY(&vd->clients)) {
24873e230dd2SCorentin Chary         qemu_del_timer(vd->timer);
24883e230dd2SCorentin Chary         qemu_free_timer(vd->timer);
24893e230dd2SCorentin Chary         vd->timer = NULL;
24903e230dd2SCorentin Chary     }
24913e230dd2SCorentin Chary }
24923e230dd2SCorentin Chary 
24933e230dd2SCorentin Chary static void vnc_connect(VncDisplay *vd, int csock)
24943e230dd2SCorentin Chary {
24953e230dd2SCorentin Chary     VncState *vs = qemu_mallocz(sizeof(VncState));
24967d964c9dSCorentin Chary     int i;
24977d964c9dSCorentin Chary 
24983e230dd2SCorentin Chary     vs->csock = csock;
24997d964c9dSCorentin Chary     vs->lossy_rect = qemu_mallocz(VNC_STAT_ROWS * sizeof (*vs->lossy_rect));
25007d964c9dSCorentin Chary     for (i = 0; i < VNC_STAT_ROWS; ++i) {
25017d964c9dSCorentin Chary         vs->lossy_rect[i] = qemu_mallocz(VNC_STAT_COLS * sizeof (uint8_t));
25027d964c9dSCorentin Chary     }
25033e230dd2SCorentin Chary 
25043e230dd2SCorentin Chary     VNC_DEBUG("New client on socket %d\n", csock);
25053e230dd2SCorentin Chary     dcl->idle = 0;
25063e230dd2SCorentin Chary     socket_set_nonblock(vs->csock);
25073e230dd2SCorentin Chary     qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
25083e230dd2SCorentin Chary 
25093e230dd2SCorentin Chary     vnc_client_cache_addr(vs);
25103e230dd2SCorentin Chary     vnc_qmp_event(vs, QEVENT_VNC_CONNECTED);
25113e230dd2SCorentin Chary 
25123e230dd2SCorentin Chary     vs->vd = vd;
25133e230dd2SCorentin Chary     vs->ds = vd->ds;
25143e230dd2SCorentin Chary     vs->last_x = -1;
25153e230dd2SCorentin Chary     vs->last_y = -1;
25163e230dd2SCorentin Chary 
25173e230dd2SCorentin Chary     vs->as.freq = 44100;
25183e230dd2SCorentin Chary     vs->as.nchannels = 2;
25193e230dd2SCorentin Chary     vs->as.fmt = AUD_FMT_S16;
25203e230dd2SCorentin Chary     vs->as.endianness = 0;
25213e230dd2SCorentin Chary 
2522bd023f95SCorentin Chary #ifdef CONFIG_VNC_THREAD
2523bd023f95SCorentin Chary     qemu_mutex_init(&vs->output_mutex);
2524bd023f95SCorentin Chary #endif
2525bd023f95SCorentin Chary 
25263e230dd2SCorentin Chary     QTAILQ_INSERT_HEAD(&vd->clients, vs, next);
25273e230dd2SCorentin Chary 
25283e230dd2SCorentin Chary     vga_hw_update();
25293e230dd2SCorentin Chary 
25303e230dd2SCorentin Chary     vnc_write(vs, "RFB 003.008\n", 12);
25313e230dd2SCorentin Chary     vnc_flush(vs);
25323e230dd2SCorentin Chary     vnc_read_when(vs, protocol_version, 12);
25333e230dd2SCorentin Chary     reset_keys(vs);
25343e230dd2SCorentin Chary     if (vs->vd->lock_key_sync)
25353e230dd2SCorentin Chary         vs->led = qemu_add_led_event_handler(kbd_leds, vs);
25363e230dd2SCorentin Chary 
25373e230dd2SCorentin Chary     vs->mouse_mode_notifier.notify = check_pointer_type_change;
25383e230dd2SCorentin Chary     qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
25393e230dd2SCorentin Chary 
25403e230dd2SCorentin Chary     vnc_init_timer(vd);
25413e230dd2SCorentin Chary 
25423e230dd2SCorentin Chary     /* vs might be free()ed here */
25433e230dd2SCorentin Chary }
25443e230dd2SCorentin Chary 
25453e230dd2SCorentin Chary static void vnc_listen_read(void *opaque)
25463e230dd2SCorentin Chary {
25473e230dd2SCorentin Chary     VncDisplay *vs = opaque;
25483e230dd2SCorentin Chary     struct sockaddr_in addr;
25493e230dd2SCorentin Chary     socklen_t addrlen = sizeof(addr);
25503e230dd2SCorentin Chary 
25513e230dd2SCorentin Chary     /* Catch-up */
25523e230dd2SCorentin Chary     vga_hw_update();
25533e230dd2SCorentin Chary 
25543e230dd2SCorentin Chary     int csock = qemu_accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
25553e230dd2SCorentin Chary     if (csock != -1) {
25563e230dd2SCorentin Chary         vnc_connect(vs, csock);
25573e230dd2SCorentin Chary     }
25583e230dd2SCorentin Chary }
25593e230dd2SCorentin Chary 
25603e230dd2SCorentin Chary void vnc_display_init(DisplayState *ds)
25613e230dd2SCorentin Chary {
25623e230dd2SCorentin Chary     VncDisplay *vs = qemu_mallocz(sizeof(*vs));
25633e230dd2SCorentin Chary 
25643e230dd2SCorentin Chary     dcl = qemu_mallocz(sizeof(DisplayChangeListener));
25653e230dd2SCorentin Chary 
25663e230dd2SCorentin Chary     ds->opaque = vs;
25673e230dd2SCorentin Chary     dcl->idle = 1;
25683e230dd2SCorentin Chary     vnc_display = vs;
25693e230dd2SCorentin Chary 
25703e230dd2SCorentin Chary     vs->lsock = -1;
25713e230dd2SCorentin Chary 
25723e230dd2SCorentin Chary     vs->ds = ds;
25733e230dd2SCorentin Chary     QTAILQ_INIT(&vs->clients);
25743c9405a0SGerd Hoffmann     vs->expires = TIME_MAX;
25753e230dd2SCorentin Chary 
25763e230dd2SCorentin Chary     if (keyboard_layout)
25773e230dd2SCorentin Chary         vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
25783e230dd2SCorentin Chary     else
25793e230dd2SCorentin Chary         vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us");
25803e230dd2SCorentin Chary 
25813e230dd2SCorentin Chary     if (!vs->kbd_layout)
25823e230dd2SCorentin Chary         exit(1);
25833e230dd2SCorentin Chary 
2584bd023f95SCorentin Chary #ifdef CONFIG_VNC_THREAD
2585bd023f95SCorentin Chary     qemu_mutex_init(&vs->mutex);
2586bd023f95SCorentin Chary     vnc_start_worker_thread();
2587bd023f95SCorentin Chary #endif
2588bd023f95SCorentin Chary 
25893e230dd2SCorentin Chary     dcl->dpy_copy = vnc_dpy_copy;
25903e230dd2SCorentin Chary     dcl->dpy_update = vnc_dpy_update;
25913e230dd2SCorentin Chary     dcl->dpy_resize = vnc_dpy_resize;
25923e230dd2SCorentin Chary     dcl->dpy_setdata = vnc_dpy_setdata;
25933e230dd2SCorentin Chary     register_displaychangelistener(ds, dcl);
25943e230dd2SCorentin Chary     ds->mouse_set = vnc_mouse_set;
25953e230dd2SCorentin Chary     ds->cursor_define = vnc_dpy_cursor_define;
25963e230dd2SCorentin Chary }
25973e230dd2SCorentin Chary 
25983e230dd2SCorentin Chary 
25993e230dd2SCorentin Chary void vnc_display_close(DisplayState *ds)
26003e230dd2SCorentin Chary {
26013e230dd2SCorentin Chary     VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
26023e230dd2SCorentin Chary 
26033e230dd2SCorentin Chary     if (!vs)
26043e230dd2SCorentin Chary         return;
26053e230dd2SCorentin Chary     if (vs->display) {
26063e230dd2SCorentin Chary         qemu_free(vs->display);
26073e230dd2SCorentin Chary         vs->display = NULL;
26083e230dd2SCorentin Chary     }
26093e230dd2SCorentin Chary     if (vs->lsock != -1) {
26103e230dd2SCorentin Chary         qemu_set_fd_handler2(vs->lsock, NULL, NULL, NULL, NULL);
26113e230dd2SCorentin Chary         close(vs->lsock);
26123e230dd2SCorentin Chary         vs->lsock = -1;
26133e230dd2SCorentin Chary     }
26143e230dd2SCorentin Chary     vs->auth = VNC_AUTH_INVALID;
26153e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
26163e230dd2SCorentin Chary     vs->subauth = VNC_AUTH_INVALID;
26173e230dd2SCorentin Chary     vs->tls.x509verify = 0;
26183e230dd2SCorentin Chary #endif
26193e230dd2SCorentin Chary }
26203e230dd2SCorentin Chary 
26211cd20f8bSAnthony Liguori int vnc_display_disable_login(DisplayState *ds)
26223e230dd2SCorentin Chary {
26233e230dd2SCorentin Chary     VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
26243e230dd2SCorentin Chary 
26253e230dd2SCorentin Chary     if (!vs) {
26263e230dd2SCorentin Chary         return -1;
26273e230dd2SCorentin Chary     }
26283e230dd2SCorentin Chary 
26293e230dd2SCorentin Chary     if (vs->password) {
26303e230dd2SCorentin Chary         qemu_free(vs->password);
26311cd20f8bSAnthony Liguori     }
26321cd20f8bSAnthony Liguori 
26331cd20f8bSAnthony Liguori     vs->password = NULL;
26341cd20f8bSAnthony Liguori     vs->auth = VNC_AUTH_VNC;
26351cd20f8bSAnthony Liguori 
26361cd20f8bSAnthony Liguori     return 0;
26371cd20f8bSAnthony Liguori }
26381cd20f8bSAnthony Liguori 
26391cd20f8bSAnthony Liguori int vnc_display_password(DisplayState *ds, const char *password)
26401cd20f8bSAnthony Liguori {
26411cd20f8bSAnthony Liguori     VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
26421cd20f8bSAnthony Liguori 
26431cd20f8bSAnthony Liguori     if (!vs) {
26441cd20f8bSAnthony Liguori         return -1;
26451cd20f8bSAnthony Liguori     }
26461cd20f8bSAnthony Liguori 
26471cd20f8bSAnthony Liguori     if (!password) {
26481cd20f8bSAnthony Liguori         /* This is not the intention of this interface but err on the side
26491cd20f8bSAnthony Liguori            of being safe */
26501cd20f8bSAnthony Liguori         return vnc_display_disable_login(ds);
26511cd20f8bSAnthony Liguori     }
26521cd20f8bSAnthony Liguori 
26531cd20f8bSAnthony Liguori     if (vs->password) {
26541cd20f8bSAnthony Liguori         qemu_free(vs->password);
26553e230dd2SCorentin Chary         vs->password = NULL;
26563e230dd2SCorentin Chary     }
26571cd20f8bSAnthony Liguori     vs->password = qemu_strdup(password);
26583e230dd2SCorentin Chary     vs->auth = VNC_AUTH_VNC;
26593e230dd2SCorentin Chary 
26603e230dd2SCorentin Chary     return 0;
26613e230dd2SCorentin Chary }
26623e230dd2SCorentin Chary 
26633c9405a0SGerd Hoffmann int vnc_display_pw_expire(DisplayState *ds, time_t expires)
26643c9405a0SGerd Hoffmann {
26653c9405a0SGerd Hoffmann     VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
26663c9405a0SGerd Hoffmann 
26673c9405a0SGerd Hoffmann     vs->expires = expires;
26683c9405a0SGerd Hoffmann     return 0;
26693c9405a0SGerd Hoffmann }
26703c9405a0SGerd Hoffmann 
26713e230dd2SCorentin Chary char *vnc_display_local_addr(DisplayState *ds)
26723e230dd2SCorentin Chary {
26733e230dd2SCorentin Chary     VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
26743e230dd2SCorentin Chary 
26753e230dd2SCorentin Chary     return vnc_socket_local_addr("%s:%s", vs->lsock);
26763e230dd2SCorentin Chary }
26773e230dd2SCorentin Chary 
26783e230dd2SCorentin Chary int vnc_display_open(DisplayState *ds, const char *display)
26793e230dd2SCorentin Chary {
26803e230dd2SCorentin Chary     VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
26813e230dd2SCorentin Chary     const char *options;
26823e230dd2SCorentin Chary     int password = 0;
26833e230dd2SCorentin Chary     int reverse = 0;
26843e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
26853e230dd2SCorentin Chary     int tls = 0, x509 = 0;
26863e230dd2SCorentin Chary #endif
26873e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
26883e230dd2SCorentin Chary     int sasl = 0;
26893e230dd2SCorentin Chary     int saslErr;
26903e230dd2SCorentin Chary #endif
26912ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
26923e230dd2SCorentin Chary     int acl = 0;
26932ded6ad7SBlue Swirl #endif
26943e230dd2SCorentin Chary     int lock_key_sync = 1;
26953e230dd2SCorentin Chary 
26963e230dd2SCorentin Chary     if (!vnc_display)
26973e230dd2SCorentin Chary         return -1;
26983e230dd2SCorentin Chary     vnc_display_close(ds);
26993e230dd2SCorentin Chary     if (strcmp(display, "none") == 0)
27003e230dd2SCorentin Chary         return 0;
27013e230dd2SCorentin Chary 
27023e230dd2SCorentin Chary     if (!(vs->display = strdup(display)))
27033e230dd2SCorentin Chary         return -1;
27043e230dd2SCorentin Chary 
27053e230dd2SCorentin Chary     options = display;
27063e230dd2SCorentin Chary     while ((options = strchr(options, ','))) {
27073e230dd2SCorentin Chary         options++;
27083e230dd2SCorentin Chary         if (strncmp(options, "password", 8) == 0) {
27093e230dd2SCorentin Chary             password = 1; /* Require password auth */
27103e230dd2SCorentin Chary         } else if (strncmp(options, "reverse", 7) == 0) {
27113e230dd2SCorentin Chary             reverse = 1;
27123e230dd2SCorentin Chary         } else if (strncmp(options, "no-lock-key-sync", 9) == 0) {
27133e230dd2SCorentin Chary             lock_key_sync = 0;
27143e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
27153e230dd2SCorentin Chary         } else if (strncmp(options, "sasl", 4) == 0) {
27163e230dd2SCorentin Chary             sasl = 1; /* Require SASL auth */
27173e230dd2SCorentin Chary #endif
27183e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
27193e230dd2SCorentin Chary         } else if (strncmp(options, "tls", 3) == 0) {
27203e230dd2SCorentin Chary             tls = 1; /* Require TLS */
27213e230dd2SCorentin Chary         } else if (strncmp(options, "x509", 4) == 0) {
27223e230dd2SCorentin Chary             char *start, *end;
27233e230dd2SCorentin Chary             x509 = 1; /* Require x509 certificates */
27243e230dd2SCorentin Chary             if (strncmp(options, "x509verify", 10) == 0)
27253e230dd2SCorentin Chary                 vs->tls.x509verify = 1; /* ...and verify client certs */
27263e230dd2SCorentin Chary 
27273e230dd2SCorentin Chary             /* Now check for 'x509=/some/path' postfix
27283e230dd2SCorentin Chary              * and use that to setup x509 certificate/key paths */
27293e230dd2SCorentin Chary             start = strchr(options, '=');
27303e230dd2SCorentin Chary             end = strchr(options, ',');
27313e230dd2SCorentin Chary             if (start && (!end || (start < end))) {
27323e230dd2SCorentin Chary                 int len = end ? end-(start+1) : strlen(start+1);
27333e230dd2SCorentin Chary                 char *path = qemu_strndup(start + 1, len);
27343e230dd2SCorentin Chary 
27353e230dd2SCorentin Chary                 VNC_DEBUG("Trying certificate path '%s'\n", path);
27363e230dd2SCorentin Chary                 if (vnc_tls_set_x509_creds_dir(vs, path) < 0) {
27373e230dd2SCorentin Chary                     fprintf(stderr, "Failed to find x509 certificates/keys in %s\n", path);
27383e230dd2SCorentin Chary                     qemu_free(path);
27393e230dd2SCorentin Chary                     qemu_free(vs->display);
27403e230dd2SCorentin Chary                     vs->display = NULL;
27413e230dd2SCorentin Chary                     return -1;
27423e230dd2SCorentin Chary                 }
27433e230dd2SCorentin Chary                 qemu_free(path);
27443e230dd2SCorentin Chary             } else {
27453e230dd2SCorentin Chary                 fprintf(stderr, "No certificate path provided\n");
27463e230dd2SCorentin Chary                 qemu_free(vs->display);
27473e230dd2SCorentin Chary                 vs->display = NULL;
27483e230dd2SCorentin Chary                 return -1;
27493e230dd2SCorentin Chary             }
27503e230dd2SCorentin Chary #endif
27512ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
27523e230dd2SCorentin Chary         } else if (strncmp(options, "acl", 3) == 0) {
27533e230dd2SCorentin Chary             acl = 1;
27542ded6ad7SBlue Swirl #endif
27553e230dd2SCorentin Chary         } else if (strncmp(options, "lossy", 5) == 0) {
27563e230dd2SCorentin Chary             vs->lossy = true;
27573e230dd2SCorentin Chary         }
27583e230dd2SCorentin Chary     }
27593e230dd2SCorentin Chary 
27603e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
27613e230dd2SCorentin Chary     if (acl && x509 && vs->tls.x509verify) {
27623e230dd2SCorentin Chary         if (!(vs->tls.acl = qemu_acl_init("vnc.x509dname"))) {
27633e230dd2SCorentin Chary             fprintf(stderr, "Failed to create x509 dname ACL\n");
27643e230dd2SCorentin Chary             exit(1);
27653e230dd2SCorentin Chary         }
27663e230dd2SCorentin Chary     }
27673e230dd2SCorentin Chary #endif
27683e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
27693e230dd2SCorentin Chary     if (acl && sasl) {
27703e230dd2SCorentin Chary         if (!(vs->sasl.acl = qemu_acl_init("vnc.username"))) {
27713e230dd2SCorentin Chary             fprintf(stderr, "Failed to create username ACL\n");
27723e230dd2SCorentin Chary             exit(1);
27733e230dd2SCorentin Chary         }
27743e230dd2SCorentin Chary     }
27753e230dd2SCorentin Chary #endif
27763e230dd2SCorentin Chary 
27773e230dd2SCorentin Chary     /*
27783e230dd2SCorentin Chary      * Combinations we support here:
27793e230dd2SCorentin Chary      *
27803e230dd2SCorentin Chary      *  - no-auth                (clear text, no auth)
27813e230dd2SCorentin Chary      *  - password               (clear text, weak auth)
27823e230dd2SCorentin Chary      *  - sasl                   (encrypt, good auth *IF* using Kerberos via GSSAPI)
27833e230dd2SCorentin Chary      *  - tls                    (encrypt, weak anonymous creds, no auth)
27843e230dd2SCorentin Chary      *  - tls + password         (encrypt, weak anonymous creds, weak auth)
27853e230dd2SCorentin Chary      *  - tls + sasl             (encrypt, weak anonymous creds, good auth)
27863e230dd2SCorentin Chary      *  - tls + x509             (encrypt, good x509 creds, no auth)
27873e230dd2SCorentin Chary      *  - tls + x509 + password  (encrypt, good x509 creds, weak auth)
27883e230dd2SCorentin Chary      *  - tls + x509 + sasl      (encrypt, good x509 creds, good auth)
27893e230dd2SCorentin Chary      *
27903e230dd2SCorentin Chary      * NB1. TLS is a stackable auth scheme.
27913e230dd2SCorentin Chary      * NB2. the x509 schemes have option to validate a client cert dname
27923e230dd2SCorentin Chary      */
27933e230dd2SCorentin Chary     if (password) {
27943e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
27953e230dd2SCorentin Chary         if (tls) {
27963e230dd2SCorentin Chary             vs->auth = VNC_AUTH_VENCRYPT;
27973e230dd2SCorentin Chary             if (x509) {
27983e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
27993e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_X509VNC;
28003e230dd2SCorentin Chary             } else {
28013e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
28023e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC;
28033e230dd2SCorentin Chary             }
28043e230dd2SCorentin Chary         } else {
28053e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
28063e230dd2SCorentin Chary             VNC_DEBUG("Initializing VNC server with password auth\n");
28073e230dd2SCorentin Chary             vs->auth = VNC_AUTH_VNC;
28083e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
28093e230dd2SCorentin Chary             vs->subauth = VNC_AUTH_INVALID;
28103e230dd2SCorentin Chary         }
28113e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
28123e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
28133e230dd2SCorentin Chary     } else if (sasl) {
28143e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
28153e230dd2SCorentin Chary         if (tls) {
28163e230dd2SCorentin Chary             vs->auth = VNC_AUTH_VENCRYPT;
28173e230dd2SCorentin Chary             if (x509) {
28183e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
28193e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_X509SASL;
28203e230dd2SCorentin Chary             } else {
28213e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
28223e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL;
28233e230dd2SCorentin Chary             }
28243e230dd2SCorentin Chary         } else {
28253e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
28263e230dd2SCorentin Chary             VNC_DEBUG("Initializing VNC server with SASL auth\n");
28273e230dd2SCorentin Chary             vs->auth = VNC_AUTH_SASL;
28283e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
28293e230dd2SCorentin Chary             vs->subauth = VNC_AUTH_INVALID;
28303e230dd2SCorentin Chary         }
28313e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
28323e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
28333e230dd2SCorentin Chary     } else {
28343e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
28353e230dd2SCorentin Chary         if (tls) {
28363e230dd2SCorentin Chary             vs->auth = VNC_AUTH_VENCRYPT;
28373e230dd2SCorentin Chary             if (x509) {
28383e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
28393e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_X509NONE;
28403e230dd2SCorentin Chary             } else {
28413e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
28423e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE;
28433e230dd2SCorentin Chary             }
28443e230dd2SCorentin Chary         } else {
28453e230dd2SCorentin Chary #endif
28463e230dd2SCorentin Chary             VNC_DEBUG("Initializing VNC server with no auth\n");
28473e230dd2SCorentin Chary             vs->auth = VNC_AUTH_NONE;
28483e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
28493e230dd2SCorentin Chary             vs->subauth = VNC_AUTH_INVALID;
28503e230dd2SCorentin Chary         }
28513e230dd2SCorentin Chary #endif
28523e230dd2SCorentin Chary     }
28533e230dd2SCorentin Chary 
28543e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
28553e230dd2SCorentin Chary     if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) {
28563e230dd2SCorentin Chary         fprintf(stderr, "Failed to initialize SASL auth %s",
28573e230dd2SCorentin Chary                 sasl_errstring(saslErr, NULL, NULL));
28583e230dd2SCorentin Chary         free(vs->display);
28593e230dd2SCorentin Chary         vs->display = NULL;
28603e230dd2SCorentin Chary         return -1;
28613e230dd2SCorentin Chary     }
28623e230dd2SCorentin Chary #endif
28633e230dd2SCorentin Chary     vs->lock_key_sync = lock_key_sync;
28643e230dd2SCorentin Chary 
28653e230dd2SCorentin Chary     if (reverse) {
28663e230dd2SCorentin Chary         /* connect to viewer */
28673e230dd2SCorentin Chary         if (strncmp(display, "unix:", 5) == 0)
28683e230dd2SCorentin Chary             vs->lsock = unix_connect(display+5);
28693e230dd2SCorentin Chary         else
28703e230dd2SCorentin Chary             vs->lsock = inet_connect(display, SOCK_STREAM);
28713e230dd2SCorentin Chary         if (-1 == vs->lsock) {
28723e230dd2SCorentin Chary             free(vs->display);
28733e230dd2SCorentin Chary             vs->display = NULL;
28743e230dd2SCorentin Chary             return -1;
28753e230dd2SCorentin Chary         } else {
28763e230dd2SCorentin Chary             int csock = vs->lsock;
28773e230dd2SCorentin Chary             vs->lsock = -1;
28783e230dd2SCorentin Chary             vnc_connect(vs, csock);
28793e230dd2SCorentin Chary         }
28803e230dd2SCorentin Chary         return 0;
28813e230dd2SCorentin Chary 
28823e230dd2SCorentin Chary     } else {
28833e230dd2SCorentin Chary         /* listen for connects */
28843e230dd2SCorentin Chary         char *dpy;
28853e230dd2SCorentin Chary         dpy = qemu_malloc(256);
28863e230dd2SCorentin Chary         if (strncmp(display, "unix:", 5) == 0) {
28873e230dd2SCorentin Chary             pstrcpy(dpy, 256, "unix:");
28883e230dd2SCorentin Chary             vs->lsock = unix_listen(display+5, dpy+5, 256-5);
28893e230dd2SCorentin Chary         } else {
28903e230dd2SCorentin Chary             vs->lsock = inet_listen(display, dpy, 256, SOCK_STREAM, 5900);
28913e230dd2SCorentin Chary         }
28923e230dd2SCorentin Chary         if (-1 == vs->lsock) {
28933e230dd2SCorentin Chary             free(dpy);
28943e230dd2SCorentin Chary             return -1;
28953e230dd2SCorentin Chary         } else {
28963e230dd2SCorentin Chary             free(vs->display);
28973e230dd2SCorentin Chary             vs->display = dpy;
28983e230dd2SCorentin Chary         }
28993e230dd2SCorentin Chary     }
29003e230dd2SCorentin Chary     return qemu_set_fd_handler2(vs->lsock, NULL, vnc_listen_read, NULL, vs);
29013e230dd2SCorentin Chary }
2902