vnc-ws.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * QEMU VNC display driver: Websockets support
  3. *
  4. * Copyright (C) 2010 Joel Martin
  5. * Copyright (C) 2012 Tim Hardeck
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this software; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qapi/error.h"
  22. #include "vnc.h"
  23. #include "io/channel-websock.h"
  24. #include "qemu/bswap.h"
  25. static void vncws_tls_handshake_done(QIOTask *task,
  26. gpointer user_data)
  27. {
  28. VncState *vs = user_data;
  29. Error *err = NULL;
  30. if (qio_task_propagate_error(task, &err)) {
  31. VNC_DEBUG("Handshake failed %s\n", error_get_pretty(err));
  32. vnc_client_error(vs);
  33. error_free(err);
  34. } else {
  35. VNC_DEBUG("TLS handshake complete, starting websocket handshake\n");
  36. vs->ioc_tag = qio_channel_add_watch(
  37. QIO_CHANNEL(vs->ioc), G_IO_IN, vncws_handshake_io, vs, NULL);
  38. }
  39. }
  40. gboolean vncws_tls_handshake_io(QIOChannel *ioc G_GNUC_UNUSED,
  41. GIOCondition condition G_GNUC_UNUSED,
  42. void *opaque)
  43. {
  44. VncState *vs = opaque;
  45. QIOChannelTLS *tls;
  46. Error *err = NULL;
  47. VNC_DEBUG("TLS Websocket connection required\n");
  48. if (vs->ioc_tag) {
  49. g_source_remove(vs->ioc_tag);
  50. vs->ioc_tag = 0;
  51. }
  52. tls = qio_channel_tls_new_server(
  53. vs->ioc,
  54. vs->vd->tlscreds,
  55. vs->vd->tlsaclname,
  56. &err);
  57. if (!tls) {
  58. VNC_DEBUG("Failed to setup TLS %s\n", error_get_pretty(err));
  59. error_free(err);
  60. vnc_client_error(vs);
  61. return TRUE;
  62. }
  63. qio_channel_set_name(QIO_CHANNEL(tls), "vnc-ws-server-tls");
  64. VNC_DEBUG("Start TLS WS handshake process\n");
  65. object_unref(OBJECT(vs->ioc));
  66. vs->ioc = QIO_CHANNEL(tls);
  67. vs->tls = qio_channel_tls_get_session(tls);
  68. qio_channel_tls_handshake(tls,
  69. vncws_tls_handshake_done,
  70. vs,
  71. NULL);
  72. return TRUE;
  73. }
  74. static void vncws_handshake_done(QIOTask *task,
  75. gpointer user_data)
  76. {
  77. VncState *vs = user_data;
  78. Error *err = NULL;
  79. if (qio_task_propagate_error(task, &err)) {
  80. VNC_DEBUG("Websock handshake failed %s\n", error_get_pretty(err));
  81. vnc_client_error(vs);
  82. error_free(err);
  83. } else {
  84. VNC_DEBUG("Websock handshake complete, starting VNC protocol\n");
  85. vnc_start_protocol(vs);
  86. vs->ioc_tag = qio_channel_add_watch(
  87. vs->ioc, G_IO_IN, vnc_client_io, vs, NULL);
  88. }
  89. }
  90. gboolean vncws_handshake_io(QIOChannel *ioc G_GNUC_UNUSED,
  91. GIOCondition condition G_GNUC_UNUSED,
  92. void *opaque)
  93. {
  94. VncState *vs = opaque;
  95. QIOChannelWebsock *wioc;
  96. VNC_DEBUG("Websocket negotiate starting\n");
  97. if (vs->ioc_tag) {
  98. g_source_remove(vs->ioc_tag);
  99. vs->ioc_tag = 0;
  100. }
  101. wioc = qio_channel_websock_new_server(vs->ioc);
  102. qio_channel_set_name(QIO_CHANNEL(wioc), "vnc-ws-server-websock");
  103. object_unref(OBJECT(vs->ioc));
  104. vs->ioc = QIO_CHANNEL(wioc);
  105. qio_channel_websock_handshake(wioc,
  106. vncws_handshake_done,
  107. vs,
  108. NULL);
  109. return TRUE;
  110. }