#include "qemu_socket.h"
#include "qemu-timer.h"
#include "acl.h"
+#include "qemu-objects.h"
#define VNC_REFRESH_INTERVAL_BASE 30
#define VNC_REFRESH_INTERVAL_INC 50
return addr_to_string(format, &sa, salen);
}
+static int put_addr_qdict(QDict *qdict, struct sockaddr_storage *sa,
+ socklen_t salen)
+{
+ char host[NI_MAXHOST];
+ char serv[NI_MAXSERV];
+ int err;
+
+ if ((err = getnameinfo((struct sockaddr *)sa, salen,
+ host, sizeof(host),
+ serv, sizeof(serv),
+ NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
+ VNC_DEBUG("Cannot resolve address %d: %s\n",
+ err, gai_strerror(err));
+ return -1;
+ }
+
+ qdict_put(qdict, "host", qstring_from_str(host));
+ qdict_put(qdict, "service", qstring_from_str(serv));
+ qdict_put(qdict, "family",qstring_from_str(inet_strfamily(sa->ss_family)));
+
+ return 0;
+}
+
+static int vnc_server_addr_put(QDict *qdict, int fd)
+{
+ struct sockaddr_storage sa;
+ socklen_t salen;
+
+ salen = sizeof(sa);
+ if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) {
+ return -1;
+ }
+
+ return put_addr_qdict(qdict, &sa, salen);
+}
+
+static int vnc_qdict_remote_addr(QDict *qdict, int fd)
+{
+ struct sockaddr_storage sa;
+ socklen_t salen;
+
+ salen = sizeof(sa);
+ if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) {
+ return -1;
+ }
+
+ return put_addr_qdict(qdict, &sa, salen);
+}
+
static const char *vnc_auth_name(VncDisplay *vd) {
switch (vd->auth) {
case VNC_AUTH_INVALID:
return "unknown";
}
-static void do_info_vnc_client(Monitor *mon, VncState *client)
+static int vnc_server_info_put(QDict *qdict)
+{
+ if (vnc_server_addr_put(qdict, vnc_display->lsock) < 0) {
+ return -1;
+ }
+
+ qdict_put(qdict, "auth", qstring_from_str(vnc_auth_name(vnc_display)));
+ return 0;
+}
+
+static void vnc_client_cache_auth(VncState *client)
{
- char *clientAddr =
- vnc_socket_remote_addr(" address: %s:%s\n",
- client->csock);
- if (!clientAddr)
+ QDict *qdict;
+
+ if (!client->info) {
return;
+ }
- monitor_printf(mon, "Client:\n");
- monitor_printf(mon, "%s", clientAddr);
- free(clientAddr);
+ qdict = qobject_to_qdict(client->info);
#ifdef CONFIG_VNC_TLS
if (client->tls.session &&
- client->tls.dname)
- monitor_printf(mon, " x509 dname: %s\n", client->tls.dname);
- else
- monitor_printf(mon, " x509 dname: none\n");
+ client->tls.dname) {
+ qdict_put(qdict, "x509_dname", qstring_from_str(client->tls.dname));
+ }
#endif
#ifdef CONFIG_VNC_SASL
if (client->sasl.conn &&
- client->sasl.username)
- monitor_printf(mon, " username: %s\n", client->sasl.username);
- else
- monitor_printf(mon, " username: none\n");
+ client->sasl.username) {
+ qdict_put(qdict, "sasl_username",
+ qstring_from_str(client->sasl.username));
+ }
#endif
}
-void do_info_vnc(Monitor *mon)
+static void vnc_client_cache_addr(VncState *client)
{
- if (vnc_display == NULL || vnc_display->display == NULL) {
+ QDict *qdict;
+
+ qdict = qdict_new();
+ if (vnc_qdict_remote_addr(qdict, client->csock) < 0) {
+ QDECREF(qdict);
+ /* XXX: how to report the error? */
+ return;
+ }
+
+ client->info = QOBJECT(qdict);
+}
+
+static void vnc_qmp_event(VncState *vs, MonitorEvent event)
+{
+ QDict *server;
+ QObject *data;
+
+ if (!vs->info) {
+ return;
+ }
+
+ server = qdict_new();
+ if (vnc_server_info_put(server) < 0) {
+ QDECREF(server);
+ return;
+ }
+
+ data = qobject_from_jsonf("{ 'client': %p, 'server': %p }",
+ vs->info, QOBJECT(server));
+
+ monitor_protocol_event(event, data);
+
+ qobject_incref(vs->info);
+ qobject_decref(data);
+}
+
+static void info_vnc_iter(QObject *obj, void *opaque)
+{
+ QDict *client;
+ Monitor *mon = opaque;
+
+ client = qobject_to_qdict(obj);
+ monitor_printf(mon, "Client:\n");
+ monitor_printf(mon, " address: %s:%s\n",
+ qdict_get_str(client, "host"),
+ qdict_get_str(client, "service"));
+
+#ifdef CONFIG_VNC_TLS
+ monitor_printf(mon, " x509_dname: %s\n",
+ qdict_haskey(client, "x509_dname") ?
+ qdict_get_str(client, "x509_dname") : "none");
+#endif
+#ifdef CONFIG_VNC_SASL
+ monitor_printf(mon, " username: %s\n",
+ qdict_haskey(client, "sasl_username") ?
+ qdict_get_str(client, "sasl_username") : "none");
+#endif
+}
+
+void do_info_vnc_print(Monitor *mon, const QObject *data)
+{
+ QDict *server;
+ QList *clients;
+
+ server = qobject_to_qdict(data);
+ if (qdict_get_bool(server, "enabled") == 0) {
monitor_printf(mon, "Server: disabled\n");
- } else {
- char *serverAddr = vnc_socket_local_addr(" address: %s:%s\n",
- vnc_display->lsock);
+ return;
+ }
- if (!serverAddr)
- return;
+ monitor_printf(mon, "Server:\n");
+ monitor_printf(mon, " address: %s:%s\n",
+ qdict_get_str(server, "host"),
+ qdict_get_str(server, "service"));
+ monitor_printf(mon, " auth: %s\n", qdict_get_str(server, "auth"));
- monitor_printf(mon, "Server:\n");
- monitor_printf(mon, "%s", serverAddr);
- free(serverAddr);
- monitor_printf(mon, " auth: %s\n", vnc_auth_name(vnc_display));
+ clients = qdict_get_qlist(server, "clients");
+ if (qlist_empty(clients)) {
+ monitor_printf(mon, "Client: none\n");
+ } else {
+ qlist_iter(clients, info_vnc_iter, mon);
+ }
+}
- if (vnc_display->clients) {
- VncState *client = vnc_display->clients;
- while (client) {
- do_info_vnc_client(mon, client);
- client = client->next;
+/**
+ * do_info_vnc(): Show VNC server information
+ *
+ * Return a QDict with server information. Connected clients are returned
+ * as a QList of QDicts.
+ *
+ * The main QDict contains the following:
+ *
+ * - "enabled": true or false
+ * - "host": server's IP address
+ * - "family": address family ("ipv4" or "ipv6")
+ * - "service": server's port number
+ * - "auth": authentication method
+ * - "clients": a QList of all connected clients
+ *
+ * Clients are described by a QDict, with the following information:
+ *
+ * - "host": client's IP address
+ * - "family": address family ("ipv4" or "ipv6")
+ * - "service": client's port number
+ * - "x509_dname": TLS dname (optional)
+ * - "sasl_username": SASL username (optional)
+ *
+ * Example:
+ *
+ * { "enabled": true, "host": "0.0.0.0", "service": "50402", "auth": "vnc",
+ * "family": "ipv4",
+ * "clients": [{ "host": "127.0.0.1", "service": "50401", "family": "ipv4" }]}
+ */
+void do_info_vnc(Monitor *mon, QObject **ret_data)
+{
+ if (vnc_display == NULL || vnc_display->display == NULL) {
+ *ret_data = qobject_from_jsonf("{ 'enabled': false }");
+ } else {
+ QList *clist;
+ VncState *client;
+
+ clist = qlist_new();
+ QTAILQ_FOREACH(client, &vnc_display->clients, next) {
+ if (client->info) {
+ /* incref so that it's not freed by upper layers */
+ qobject_incref(client->info);
+ qlist_append_obj(clist, client->info);
}
- } else {
- monitor_printf(mon, "Client: none\n");
+ }
+
+ *ret_data = qobject_from_jsonf("{ 'enabled': true, 'clients': %p }",
+ QOBJECT(clist));
+ assert(*ret_data != NULL);
+
+ if (vnc_server_info_put(qobject_to_qdict(*ret_data)) < 0) {
+ qobject_decref(*ret_data);
+ *ret_data = NULL;
}
}
}
{
int size_changed;
VncDisplay *vd = ds->opaque;
- VncState *vs = vd->clients;
+ VncState *vs;
/* server surface */
if (!vd->server)
*(vd->guest.ds) = *(ds->surface);
memset(vd->guest.dirty, 0xFF, sizeof(vd->guest.dirty));
- while (vs != NULL) {
+ QTAILQ_FOREACH(vs, &vd->clients, next) {
vnc_colordepth(vs);
if (size_changed) {
if (vs->csock != -1 && vnc_has_feature(vs, VNC_FEATURE_RESIZE)) {
}
}
memset(vs->dirty, 0xFF, sizeof(vs->dirty));
- vs = vs->next;
}
}
}
+#define ZALLOC_ALIGNMENT 16
+
+static void *zalloc(void *x, unsigned items, unsigned size)
+{
+ void *p;
+
+ size *= items;
+ size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
+
+ p = qemu_mallocz(size);
+
+ return (p);
+}
+
+static void zfree(void *x, void *addr)
+{
+ qemu_free(addr);
+}
+
static void vnc_zlib_init(VncState *vs)
{
int i;
VNC_DEBUG("VNC: initializing zlib stream %d\n", stream_id);
VNC_DEBUG("VNC: opaque = %p | vs = %p\n", zstream->opaque, vs);
- zstream->zalloc = Z_NULL;
- zstream->zfree = Z_NULL;
+ zstream->zalloc = zalloc;
+ zstream->zfree = zfree;
err = deflateInit2(zstream, vs->tight_compression, Z_DEFLATED, MAX_WBITS,
MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
int cmp_bytes;
vnc_refresh_server_surface(vd);
- for (vs = vd->clients; vs != NULL; vs = vn) {
- vn = vs->next;
+ QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
vs->force_update = 1;
vnc_update_client(vs, 1);
if (memcmp(src_row, dst_row, cmp_bytes) == 0)
continue;
memmove(dst_row, src_row, cmp_bytes);
- vs = vd->clients;
- while (vs != NULL) {
- if (!vnc_has_feature(vs, VNC_FEATURE_COPYRECT))
+ QTAILQ_FOREACH(vs, &vd->clients, next) {
+ if (!vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
vnc_set_bit(vs->dirty[y], ((x + dst_x) / 16));
- vs = vs->next;
+ }
}
}
src_row += pitch - w * depth;
y += inc;
}
- for (vs = vd->clients; vs != NULL; vs = vs->next) {
- if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT))
+ QTAILQ_FOREACH(vs, &vd->clients, next) {
+ if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h);
+ }
}
}
static void vnc_disconnect_finish(VncState *vs)
{
+ vnc_qmp_event(vs, QEVENT_VNC_DISCONNECTED);
+
if (vs->input.buffer) {
qemu_free(vs->input.buffer);
vs->input.buffer = NULL;
qemu_free(vs->output.buffer);
vs->output.buffer = NULL;
}
+
+ qobject_decref(vs->info);
+
#ifdef CONFIG_VNC_TLS
vnc_tls_client_cleanup(vs);
#endif /* CONFIG_VNC_TLS */
#endif /* CONFIG_VNC_SASL */
audio_del(vs);
- VncState *p, *parent = NULL;
- for (p = vs->vd->clients; p != NULL; p = p->next) {
- if (p == vs) {
- if (parent)
- parent->next = p->next;
- else
- vs->vd->clients = p->next;
- break;
- }
- parent = p;
- }
- if (!vs->vd->clients)
+ QTAILQ_REMOVE(&vs->vd->clients, vs, next);
+
+ if (QTAILQ_EMPTY(&vs->vd->clients)) {
dcl->idle = 1;
+ }
vnc_remove_timer(vs->vd);
qemu_free(vs);
}
}
+ if ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z')) {
+ /* If the capslock state needs to change then simulate an additional
+ keypress before sending this one. This will happen if the user
+ toggles capslock away from the VNC window.
+ */
+ int uppercase = !!(sym >= 'A' && sym <= 'Z');
+ int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]);
+ int capslock = !!(vs->modifiers_state[0x3a]);
+ if (capslock) {
+ if (uppercase == shift) {
+ vs->modifiers_state[0x3a] = 0;
+ press_key(vs, 0xffe5);
+ }
+ } else {
+ if (uppercase != shift) {
+ vs->modifiers_state[0x3a] = 1;
+ press_key(vs, 0xffe5);
+ }
+ }
+ }
+
if (is_graphic_console()) {
if (keycode & 0x80)
kbd_put_keycode(0xe0);
static void key_event(VncState *vs, int down, uint32_t sym)
{
int keycode;
+ int lsym = sym;
- if (sym >= 'A' && sym <= 'Z' && is_graphic_console())
- sym = sym - 'A' + 'a';
+ if (lsym >= 'A' && lsym <= 'Z' && is_graphic_console()) {
+ lsym = lsym - 'A' + 'a';
+ }
- keycode = keysym2scancode(vs->vd->kbd_layout, sym & 0xFFFF);
+ keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF);
do_key_event(vs, down, keycode, sym);
}
vnc_write(vs, buf, size);
vnc_flush(vs);
+ vnc_client_cache_auth(vs);
+ vnc_qmp_event(vs, QEVENT_VNC_INITIALIZED);
+
vnc_read_when(vs, protocol_client_msg, 1);
return 0;
uint8_t *server_row;
int cmp_bytes;
uint32_t width_mask[VNC_DIRTY_WORDS];
- VncState *vs = NULL;
+ VncState *vs;
int has_dirty = 0;
/*
if (memcmp(server_ptr, guest_ptr, cmp_bytes) == 0)
continue;
memcpy(server_ptr, guest_ptr, cmp_bytes);
- vs = vd->clients;
- while (vs != NULL) {
+ QTAILQ_FOREACH(vs, &vd->clients, next) {
vnc_set_bit(vs->dirty[y], (x / 16));
- vs = vs->next;
}
has_dirty++;
}
static void vnc_refresh(void *opaque)
{
VncDisplay *vd = opaque;
- VncState *vs = NULL;
- int has_dirty = 0, rects = 0;
+ VncState *vs, *vn;
+ int has_dirty, rects = 0;
vga_hw_update();
has_dirty = vnc_refresh_server_surface(vd);
- vs = vd->clients;
- while (vs != NULL) {
+ QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
rects += vnc_update_client(vs, has_dirty);
- vs = vs->next;
+ /* vs might be free()ed here */
}
+ /* vd->timer could be NULL now if the last client disconnected,
+ * in this case don't update the timer */
+ if (vd->timer == NULL)
+ return;
if (has_dirty && rects) {
vd->timer_interval /= 2;
static void vnc_init_timer(VncDisplay *vd)
{
vd->timer_interval = VNC_REFRESH_INTERVAL_BASE;
- if (vd->timer == NULL && vd->clients != NULL) {
+ if (vd->timer == NULL && !QTAILQ_EMPTY(&vd->clients)) {
vd->timer = qemu_new_timer(rt_clock, vnc_refresh, vd);
vnc_refresh(vd);
}
static void vnc_remove_timer(VncDisplay *vd)
{
- if (vd->timer != NULL && vd->clients == NULL) {
+ if (vd->timer != NULL && QTAILQ_EMPTY(&vd->clients)) {
qemu_del_timer(vd->timer);
qemu_free_timer(vd->timer);
vd->timer = NULL;
socket_set_nonblock(vs->csock);
qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
+ vnc_client_cache_addr(vs);
+ vnc_qmp_event(vs, QEVENT_VNC_CONNECTED);
+
vs->vd = vd;
vs->ds = vd->ds;
vs->last_x = -1;
vs->as.fmt = AUD_FMT_S16;
vs->as.endianness = 0;
- vs->next = vd->clients;
- vd->clients = vs;
+ QTAILQ_INSERT_HEAD(&vd->clients, vs, next);
vga_hw_update();
/* Catch-up */
vga_hw_update();
- int csock = accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
+ int csock = qemu_accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
if (csock != -1) {
vnc_connect(vs, csock);
}
vs->lsock = -1;
vs->ds = ds;
+ QTAILQ_INIT(&vs->clients);
if (keyboard_layout)
vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
const char *options;
int password = 0;
int reverse = 0;
- int to_port = 0;
#ifdef CONFIG_VNC_TLS
int tls = 0, x509 = 0;
#endif
password = 1; /* Require password auth */
} else if (strncmp(options, "reverse", 7) == 0) {
reverse = 1;
- } else if (strncmp(options, "to=", 3) == 0) {
- to_port = atoi(options+3) + 5900;
#ifdef CONFIG_VNC_SASL
} else if (strncmp(options, "sasl", 4) == 0) {
sasl = 1; /* Require SASL auth */