summaryrefslogtreecommitdiff
path: root/include/kvm_websocket.hpp
blob: aa2eaecc3205089f17cb7b3017f682e62e9731f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#pragma once
#include <crow/app.h>
#include <crow/websocket.h>
#include <sys/socket.h>

#include <boost/container/flat_map.hpp>
#include <boost/container/flat_set.hpp>
#include <webserver_common.hpp>

namespace crow
{
namespace obmc_kvm
{

static std::unique_ptr<boost::asio::ip::tcp::socket> hostSocket;

// TODO(ed) validate that these buffer sizes are sane
static boost::beast::flat_static_buffer<1024U * 50U> outputBuffer;
static boost::beast::flat_static_buffer<1024U> inputBuffer;

static crow::websocket::Connection* session = nullptr;

static bool doingWrite = false;

inline void doWrite()
{
    if (doingWrite)
    {
        BMCWEB_LOG_DEBUG << "Already writing.  Bailing out";
        return;
    }
    if (inputBuffer.size() == 0)
    {
        BMCWEB_LOG_DEBUG << "inputBuffer empty.  Bailing out";
        return;
    }

    doingWrite = true;
    hostSocket->async_write_some(
        inputBuffer.data(),
        [](boost::beast::error_code ec, std::size_t bytes_written) {
            BMCWEB_LOG_DEBUG << "Wrote " << bytes_written << "bytes";
            doingWrite = false;
            inputBuffer.consume(bytes_written);

            if (session == nullptr)
            {
                return;
            }
            if (ec == boost::asio::error::eof)
            {
                session->close("KVM socket port closed");
                return;
            }
            if (ec)
            {
                session->close("Error in reading to host port");
                BMCWEB_LOG_ERROR << "Error in KVM socket write " << ec;
                return;
            }
            doWrite();
        });
}

inline void doRead();

inline void readDone(const boost::system::error_code& ec, std::size_t bytesRead)
{
    outputBuffer.commit(bytesRead);
    BMCWEB_LOG_DEBUG << "read done.  Read " << bytesRead << " bytes";
    if (ec)
    {
        BMCWEB_LOG_ERROR << "Couldn't read from KVM socket port: " << ec;
        if (session != nullptr)
        {
            session->close("Error in connecting to KVM port");
        }
        return;
    }
    if (session == nullptr)
    {
        return;
    }

    boost::beast::string_view payload(
        static_cast<const char*>(outputBuffer.data().data()), bytesRead);
    BMCWEB_LOG_DEBUG << "Sending payload size " << payload.size();
    session->sendBinary(payload);
    outputBuffer.consume(bytesRead);

    doRead();
}

inline void doRead()
{
    std::size_t bytes = outputBuffer.capacity() - outputBuffer.size();
    BMCWEB_LOG_DEBUG << "Reading " << bytes << " from kvm socket";
    hostSocket->async_read_some(
        outputBuffer.prepare(outputBuffer.capacity() - outputBuffer.size()),
        readDone);
}

inline void connectHandler(const boost::system::error_code& ec)
{
    if (ec)
    {
        BMCWEB_LOG_ERROR << "Couldn't connect to KVM socket port: " << ec;
        if (session != nullptr)
        {
            session->close("Error in connecting to KVM port");
        }
        return;
    }

    doWrite();
    doRead();
}

inline void requestRoutes(CrowApp& app)
{
    BMCWEB_ROUTE(app, "/kvm/0")
        .websocket()
        .onopen([](crow::websocket::Connection& conn) {
            BMCWEB_LOG_DEBUG << "Connection " << &conn << " opened";

            if (session != nullptr)
            {
                conn.close("User already connected");
                return;
            }

            session = &conn;
            if (hostSocket == nullptr)
            {
                boost::asio::ip::tcp::endpoint endpoint(
                    boost::asio::ip::address::from_string("127.0.0.1"), 5900);

                hostSocket = std::make_unique<boost::asio::ip::tcp::socket>(
                    conn.get_io_context());
                hostSocket->async_connect(endpoint, connectHandler);
            }
        })
        .onclose(
            [](crow::websocket::Connection& conn, const std::string& reason) {
                session = nullptr;
                hostSocket = nullptr;
                inputBuffer.reset();
                outputBuffer.reset();
            })
        .onmessage([](crow::websocket::Connection& conn,
                      const std::string& data, bool is_binary) {
            if (data.length() > inputBuffer.capacity())
            {
                BMCWEB_LOG_ERROR << "Buffer overrun when writing "
                                 << data.length() << " bytes";
                conn.close("Buffer overrun");
                return;
            }

            BMCWEB_LOG_DEBUG << "Read " << data.size()
                             << " bytes from websocket";
            boost::asio::buffer_copy(inputBuffer.prepare(data.size()),
                                     boost::asio::buffer(data));
            BMCWEB_LOG_DEBUG << "commiting " << data.size()
                             << " bytes from websocket";
            inputBuffer.commit(data.size());

            BMCWEB_LOG_DEBUG << "inputbuffer size " << inputBuffer.size();
            doWrite();
        });
}
} // namespace obmc_kvm
} // namespace crow