2
0

vnc-ws.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #include "trace.h"
  26. static void vncws_tls_handshake_done(QIOTask *task,
  27. gpointer user_data)
  28. {
  29. VncState *vs = user_data;
  30. Error *err = NULL;
  31. if (qio_task_propagate_error(task, &err)) {
  32. VNC_DEBUG("Handshake failed %s\n", error_get_pretty(err));
  33. vnc_client_error(vs);
  34. error_free(err);
  35. } else {
  36. VNC_DEBUG("TLS handshake complete, starting websocket handshake\n");
  37. if (vs->ioc_tag) {
  38. g_source_remove(vs->ioc_tag);
  39. }
  40. vs->ioc_tag = qio_channel_add_watch(
  41. QIO_CHANNEL(vs->ioc), G_IO_IN, vncws_handshake_io, vs, NULL);
  42. }
  43. }
  44. gboolean vncws_tls_handshake_io(QIOChannel *ioc G_GNUC_UNUSED,
  45. GIOCondition condition G_GNUC_UNUSED,
  46. void *opaque)
  47. {
  48. VncState *vs = opaque;
  49. QIOChannelTLS *tls;
  50. Error *err = NULL;
  51. if (vs->ioc_tag) {
  52. g_source_remove(vs->ioc_tag);
  53. vs->ioc_tag = 0;
  54. }
  55. tls = qio_channel_tls_new_server(
  56. vs->ioc,
  57. vs->vd->tlscreds,
  58. vs->vd->tlsauthzid,
  59. &err);
  60. if (!tls) {
  61. VNC_DEBUG("Failed to setup TLS %s\n", error_get_pretty(err));
  62. error_free(err);
  63. vnc_client_error(vs);
  64. return TRUE;
  65. }
  66. qio_channel_set_name(QIO_CHANNEL(tls), "vnc-ws-server-tls");
  67. object_unref(OBJECT(vs->ioc));
  68. vs->ioc = QIO_CHANNEL(tls);
  69. trace_vnc_client_io_wrap(vs, vs->ioc, "tls");
  70. vs->tls = qio_channel_tls_get_session(tls);
  71. qio_channel_tls_handshake(tls,
  72. vncws_tls_handshake_done,
  73. vs,
  74. NULL,
  75. NULL);
  76. return TRUE;
  77. }
  78. static void vncws_handshake_done(QIOTask *task,
  79. gpointer user_data)
  80. {
  81. VncState *vs = user_data;
  82. Error *err = NULL;
  83. if (qio_task_propagate_error(task, &err)) {
  84. VNC_DEBUG("Websock handshake failed %s\n", error_get_pretty(err));
  85. vnc_client_error(vs);
  86. error_free(err);
  87. } else {
  88. VNC_DEBUG("Websock handshake complete, starting VNC protocol\n");
  89. vnc_start_protocol(vs);
  90. if (vs->ioc_tag) {
  91. g_source_remove(vs->ioc_tag);
  92. }
  93. vs->ioc_tag = qio_channel_add_watch(
  94. vs->ioc, G_IO_IN, vnc_client_io, vs, NULL);
  95. }
  96. }
  97. gboolean vncws_handshake_io(QIOChannel *ioc G_GNUC_UNUSED,
  98. GIOCondition condition G_GNUC_UNUSED,
  99. void *opaque)
  100. {
  101. VncState *vs = opaque;
  102. QIOChannelWebsock *wioc;
  103. if (vs->ioc_tag) {
  104. g_source_remove(vs->ioc_tag);
  105. vs->ioc_tag = 0;
  106. }
  107. wioc = qio_channel_websock_new_server(vs->ioc);
  108. qio_channel_set_name(QIO_CHANNEL(wioc), "vnc-ws-server-websock");
  109. object_unref(OBJECT(vs->ioc));
  110. vs->ioc = QIO_CHANNEL(wioc);
  111. trace_vnc_client_io_wrap(vs, vs->ioc, "websock");
  112. qio_channel_websock_handshake(wioc,
  113. vncws_handshake_done,
  114. vs,
  115. NULL);
  116. return TRUE;
  117. }