Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU I/O channels driver websockets |
| 3 | * |
| 4 | * Copyright (c) 2015 Red Hat, Inc. |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
Chetan Pant | c8198bd | 2020-10-14 13:40:33 +0000 | [diff] [blame] | 9 | * version 2.1 of the License, or (at your option) any later version. |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
Markus Armbruster | 2a6a407 | 2016-06-29 13:47:03 +0200 | [diff] [blame] | 21 | #ifndef QIO_CHANNEL_WEBSOCK_H |
| 22 | #define QIO_CHANNEL_WEBSOCK_H |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 23 | |
| 24 | #include "io/channel.h" |
| 25 | #include "qemu/buffer.h" |
| 26 | #include "io/task.h" |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 27 | #include "qom/object.h" |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 28 | |
| 29 | #define TYPE_QIO_CHANNEL_WEBSOCK "qio-channel-websock" |
Eduardo Habkost | 8063396 | 2020-09-16 14:25:19 -0400 | [diff] [blame] | 30 | OBJECT_DECLARE_SIMPLE_TYPE(QIOChannelWebsock, QIO_CHANNEL_WEBSOCK) |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 31 | |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 32 | typedef union QIOChannelWebsockMask QIOChannelWebsockMask; |
| 33 | |
| 34 | union QIOChannelWebsockMask { |
| 35 | char c[4]; |
| 36 | uint32_t u; |
| 37 | }; |
| 38 | |
| 39 | /** |
| 40 | * QIOChannelWebsock |
| 41 | * |
| 42 | * The QIOChannelWebsock class provides a channel wrapper which |
| 43 | * can transparently run the HTTP websockets protocol. This is |
| 44 | * usually used over a TCP socket, but there is actually no |
| 45 | * technical restriction on which type of master channel is |
| 46 | * used as the transport. |
| 47 | * |
| 48 | * This channel object is currently only capable of running as |
| 49 | * a websocket server and is a pretty crude implementation |
| 50 | * of it, not supporting the full websockets protocol feature |
| 51 | * set. It is sufficient to use with a simple websockets |
| 52 | * client for encapsulating VNC for noVNC in-browser client. |
| 53 | */ |
| 54 | |
| 55 | struct QIOChannelWebsock { |
| 56 | QIOChannel parent; |
| 57 | QIOChannel *master; |
| 58 | Buffer encinput; |
| 59 | Buffer encoutput; |
| 60 | Buffer rawinput; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 61 | size_t payload_remain; |
Daniel P. Berrange | 57b0cdf | 2017-10-09 15:34:06 +0100 | [diff] [blame] | 62 | size_t pong_remain; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 63 | QIOChannelWebsockMask mask; |
| 64 | guint io_tag; |
| 65 | Error *io_err; |
| 66 | gboolean io_eof; |
Brandon Carpenter | ff1300e | 2017-09-12 08:21:49 -0700 | [diff] [blame] | 67 | uint8_t opcode; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | /** |
| 71 | * qio_channel_websock_new_server: |
| 72 | * @master: the underlying channel object |
| 73 | * |
| 74 | * Create a new websockets channel that runs the server |
| 75 | * side of the protocol. |
| 76 | * |
| 77 | * After creating the channel, it is mandatory to call |
| 78 | * the qio_channel_websock_handshake() method before attempting |
| 79 | * todo any I/O on the channel. |
| 80 | * |
| 81 | * Once the handshake has completed, all I/O should be done |
| 82 | * via the new websocket channel object and not the original |
| 83 | * master channel |
| 84 | * |
| 85 | * Returns: the new websockets channel object |
| 86 | */ |
| 87 | QIOChannelWebsock * |
| 88 | qio_channel_websock_new_server(QIOChannel *master); |
| 89 | |
| 90 | /** |
| 91 | * qio_channel_websock_handshake: |
| 92 | * @ioc: the websocket channel object |
| 93 | * @func: the callback to invoke when completed |
| 94 | * @opaque: opaque data to pass to @func |
| 95 | * @destroy: optional callback to free @opaque |
| 96 | * |
| 97 | * Perform the websocket handshake. This method |
| 98 | * will return immediately and the handshake will |
| 99 | * continue in the background, provided the main |
| 100 | * loop is running. When the handshake is complete, |
| 101 | * or fails, the @func callback will be invoked. |
| 102 | */ |
| 103 | void qio_channel_websock_handshake(QIOChannelWebsock *ioc, |
| 104 | QIOTaskFunc func, |
| 105 | gpointer opaque, |
| 106 | GDestroyNotify destroy); |
| 107 | |
Markus Armbruster | 2a6a407 | 2016-06-29 13:47:03 +0200 | [diff] [blame] | 108 | #endif /* QIO_CHANNEL_WEBSOCK_H */ |