xref: /qemu/include/io/channel-websock.h (revision 9277d81f)
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
9  * version 2 of the License, or (at your option) any later version.
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 
21 #ifndef QIO_CHANNEL_WEBSOCK_H
22 #define QIO_CHANNEL_WEBSOCK_H
23 
24 #include "io/channel.h"
25 #include "qemu/buffer.h"
26 #include "io/task.h"
27 
28 #define TYPE_QIO_CHANNEL_WEBSOCK "qio-channel-websock"
29 #define QIO_CHANNEL_WEBSOCK(obj)                                     \
30     OBJECT_CHECK(QIOChannelWebsock, (obj), TYPE_QIO_CHANNEL_WEBSOCK)
31 
32 typedef struct QIOChannelWebsock QIOChannelWebsock;
33 typedef union QIOChannelWebsockMask QIOChannelWebsockMask;
34 
35 union QIOChannelWebsockMask {
36     char c[4];
37     uint32_t u;
38 };
39 
40 /**
41  * QIOChannelWebsock
42  *
43  * The QIOChannelWebsock class provides a channel wrapper which
44  * can transparently run the HTTP websockets protocol. This is
45  * usually used over a TCP socket, but there is actually no
46  * technical restriction on which type of master channel is
47  * used as the transport.
48  *
49  * This channel object is currently only capable of running as
50  * a websocket server and is a pretty crude implementation
51  * of it, not supporting the full websockets protocol feature
52  * set. It is sufficient to use with a simple websockets
53  * client for encapsulating VNC for noVNC in-browser client.
54  */
55 
56 struct QIOChannelWebsock {
57     QIOChannel parent;
58     QIOChannel *master;
59     Buffer encinput;
60     Buffer encoutput;
61     Buffer rawinput;
62     size_t payload_remain;
63     size_t pong_remain;
64     QIOChannelWebsockMask mask;
65     guint io_tag;
66     Error *io_err;
67     gboolean io_eof;
68     uint8_t opcode;
69 };
70 
71 /**
72  * qio_channel_websock_new_server:
73  * @master: the underlying channel object
74  *
75  * Create a new websockets channel that runs the server
76  * side of the protocol.
77  *
78  * After creating the channel, it is mandatory to call
79  * the qio_channel_websock_handshake() method before attempting
80  * todo any I/O on the channel.
81  *
82  * Once the handshake has completed, all I/O should be done
83  * via the new websocket channel object and not the original
84  * master channel
85  *
86  * Returns: the new websockets channel object
87  */
88 QIOChannelWebsock *
89 qio_channel_websock_new_server(QIOChannel *master);
90 
91 /**
92  * qio_channel_websock_handshake:
93  * @ioc: the websocket channel object
94  * @func: the callback to invoke when completed
95  * @opaque: opaque data to pass to @func
96  * @destroy: optional callback to free @opaque
97  *
98  * Perform the websocket handshake. This method
99  * will return immediately and the handshake will
100  * continue in the background, provided the main
101  * loop is running. When the handshake is complete,
102  * or fails, the @func callback will be invoked.
103  */
104 void qio_channel_websock_handshake(QIOChannelWebsock *ioc,
105                                    QIOTaskFunc func,
106                                    gpointer opaque,
107                                    GDestroyNotify destroy);
108 
109 #endif /* QIO_CHANNEL_WEBSOCK_H */
110