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