Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2019 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 17 | #include "app.hpp" |
| 18 | #include "dbus_utility.hpp" |
| 19 | #include "privileges.hpp" |
Ed Tanous | faf100f | 2023-05-25 10:03:14 -0700 | [diff] [blame] | 20 | #include "websocket.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 21 | |
Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 22 | #include <boost/asio/local/stream_protocol.hpp> |
| 23 | #include <boost/asio/write.hpp> |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 24 | #include <boost/beast/core/buffers_to_string.hpp> |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 25 | #include <boost/container/flat_map.hpp> |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 26 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 27 | #include <string_view> |
| 28 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 29 | namespace crow |
| 30 | { |
| 31 | |
| 32 | namespace nbd_proxy |
| 33 | { |
| 34 | |
| 35 | using boost::asio::local::stream_protocol; |
| 36 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 37 | static constexpr size_t nbdBufferSize = 131088; |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 38 | constexpr const char* requiredPrivilegeString = "ConfigureManager"; |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 39 | |
| 40 | struct NbdProxyServer : std::enable_shared_from_this<NbdProxyServer> |
| 41 | { |
| 42 | NbdProxyServer(crow::websocket::Connection& connIn, |
| 43 | const std::string& socketIdIn, |
| 44 | const std::string& endpointIdIn, const std::string& pathIn) : |
| 45 | socketId(socketIdIn), |
| 46 | endpointId(endpointIdIn), path(pathIn), |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 47 | |
| 48 | peerSocket(connIn.getIoContext()), |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 49 | acceptor(connIn.getIoContext(), stream_protocol::endpoint(socketId)), |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 50 | connection(connIn) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 51 | {} |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 52 | |
Ed Tanous | 08e9fa9 | 2022-02-16 16:18:08 -0800 | [diff] [blame] | 53 | NbdProxyServer(const NbdProxyServer&) = delete; |
| 54 | NbdProxyServer(NbdProxyServer&&) = delete; |
| 55 | NbdProxyServer& operator=(const NbdProxyServer&) = delete; |
| 56 | NbdProxyServer& operator=(NbdProxyServer&&) = delete; |
| 57 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 58 | ~NbdProxyServer() |
| 59 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 60 | BMCWEB_LOG_DEBUG("NbdProxyServer destructor"); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 61 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 62 | BMCWEB_LOG_DEBUG("peerSocket->close()"); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 63 | boost::system::error_code ec; |
| 64 | peerSocket.close(ec); |
| 65 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 66 | BMCWEB_LOG_DEBUG("std::remove({})", socketId); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 67 | std::remove(socketId.c_str()); |
| 68 | |
| 69 | crow::connections::systemBus->async_method_call( |
| 70 | dbus::utility::logError, "xyz.openbmc_project.VirtualMedia", path, |
| 71 | "xyz.openbmc_project.VirtualMedia.Proxy", "Unmount"); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | std::string getEndpointId() const |
| 75 | { |
| 76 | return endpointId; |
| 77 | } |
| 78 | |
| 79 | void run() |
| 80 | { |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 81 | acceptor.async_accept( |
| 82 | [weak(weak_from_this())](const boost::system::error_code& ec, |
| 83 | stream_protocol::socket socket) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 84 | if (ec) |
| 85 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 86 | BMCWEB_LOG_ERROR("UNIX socket: async_accept error = {}", |
| 87 | ec.message()); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 88 | return; |
| 89 | } |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 90 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 91 | BMCWEB_LOG_DEBUG("Connection opened"); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 92 | std::shared_ptr<NbdProxyServer> self = weak.lock(); |
| 93 | if (self == nullptr) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 94 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 95 | return; |
| 96 | } |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 97 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 98 | self->connection.resumeRead(); |
| 99 | self->peerSocket = std::move(socket); |
| 100 | // Start reading from socket |
| 101 | self->doRead(); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 102 | }); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 103 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 104 | auto mountHandler = [weak(weak_from_this())]( |
| 105 | const boost::system::error_code& ec, bool) { |
| 106 | std::shared_ptr<NbdProxyServer> self = weak.lock(); |
| 107 | if (self == nullptr) |
| 108 | { |
| 109 | return; |
| 110 | } |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 111 | if (ec) |
| 112 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 113 | BMCWEB_LOG_ERROR("DBus error: cannot call mount method = {}", |
| 114 | ec.message()); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 115 | |
| 116 | self->connection.close("Failed to mount media"); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 117 | return; |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | crow::connections::systemBus->async_method_call( |
| 122 | std::move(mountHandler), "xyz.openbmc_project.VirtualMedia", path, |
| 123 | "xyz.openbmc_project.VirtualMedia.Proxy", "Mount"); |
| 124 | } |
| 125 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 126 | void send(std::string_view buffer, std::function<void()>&& onDone) |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 127 | { |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 128 | boost::asio::buffer_copy(ws2uxBuf.prepare(buffer.size()), |
| 129 | boost::asio::buffer(buffer)); |
| 130 | ws2uxBuf.commit(buffer.size()); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 131 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 132 | doWrite(std::move(onDone)); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | private: |
| 136 | void doRead() |
| 137 | { |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 138 | // Trigger async read |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 139 | peerSocket.async_read_some( |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 140 | ux2wsBuf.prepare(nbdBufferSize), |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 141 | [weak(weak_from_this())](const boost::system::error_code& ec, |
| 142 | size_t bytesRead) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 143 | if (ec) |
| 144 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 145 | BMCWEB_LOG_ERROR("UNIX socket: async_read_some error = {}", |
| 146 | ec.message()); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 147 | return; |
| 148 | } |
| 149 | std::shared_ptr<NbdProxyServer> self = weak.lock(); |
| 150 | if (self == nullptr) |
| 151 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 152 | return; |
| 153 | } |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 154 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 155 | // Send to websocket |
| 156 | self->ux2wsBuf.commit(bytesRead); |
| 157 | self->connection.sendEx( |
| 158 | crow::websocket::MessageType::Binary, |
| 159 | boost::beast::buffers_to_string(self->ux2wsBuf.data()), |
| 160 | [weak(self->weak_from_this())]() { |
| 161 | std::shared_ptr<NbdProxyServer> self2 = weak.lock(); |
| 162 | if (self2 != nullptr) |
| 163 | { |
| 164 | self2->ux2wsBuf.consume(self2->ux2wsBuf.size()); |
| 165 | self2->doRead(); |
| 166 | } |
| 167 | }); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 168 | }); |
| 169 | } |
| 170 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 171 | void doWrite(std::function<void()>&& onDone) |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 172 | { |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 173 | if (uxWriteInProgress) |
| 174 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 175 | BMCWEB_LOG_ERROR("Write in progress"); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 176 | return; |
| 177 | } |
| 178 | |
Jason M. Bills | 9c0b4e5 | 2022-02-14 07:23:30 -0800 | [diff] [blame] | 179 | if (ws2uxBuf.size() == 0) |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 180 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 181 | BMCWEB_LOG_ERROR("No data to write to UNIX socket"); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 182 | return; |
| 183 | } |
| 184 | |
| 185 | uxWriteInProgress = true; |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 186 | peerSocket.async_write_some( |
| 187 | ws2uxBuf.data(), |
| 188 | [weak(weak_from_this()), |
| 189 | onDone(std::move(onDone))](const boost::system::error_code& ec, |
| 190 | size_t bytesWritten) mutable { |
| 191 | std::shared_ptr<NbdProxyServer> self = weak.lock(); |
| 192 | if (self == nullptr) |
| 193 | { |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | self->ws2uxBuf.consume(bytesWritten); |
| 198 | self->uxWriteInProgress = false; |
| 199 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 200 | if (ec) |
| 201 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 202 | BMCWEB_LOG_ERROR("UNIX: async_write error = {}", ec.message()); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 203 | self->connection.close("Internal error"); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 204 | return; |
| 205 | } |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 206 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 207 | // Retrigger doWrite if there is something in buffer |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 208 | if (self->ws2uxBuf.size() > 0) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 209 | { |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 210 | self->doWrite(std::move(onDone)); |
| 211 | return; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 212 | } |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 213 | onDone(); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 214 | }); |
| 215 | } |
| 216 | |
| 217 | // Keeps UNIX socket endpoint file path |
| 218 | const std::string socketId; |
| 219 | const std::string endpointId; |
| 220 | const std::string path; |
| 221 | |
| 222 | bool uxWriteInProgress = false; |
| 223 | |
| 224 | // UNIX => WebSocket buffer |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 225 | boost::beast::flat_static_buffer<nbdBufferSize> ux2wsBuf; |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 226 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 227 | // WebSocket => UNIX buffer |
| 228 | boost::beast::flat_static_buffer<nbdBufferSize> ws2uxBuf; |
| 229 | |
| 230 | // The socket used to communicate with the client. |
| 231 | stream_protocol::socket peerSocket; |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 232 | |
| 233 | // Default acceptor for UNIX socket |
| 234 | stream_protocol::acceptor acceptor; |
| 235 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 236 | crow::websocket::Connection& connection; |
| 237 | }; |
| 238 | |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 239 | using SessionMap = boost::container::flat_map<crow::websocket::Connection*, |
| 240 | std::shared_ptr<NbdProxyServer>>; |
| 241 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
| 242 | static SessionMap sessions; |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 243 | |
Ed Tanous | 2da6b8c | 2023-02-27 14:25:10 -0800 | [diff] [blame] | 244 | inline void |
| 245 | afterGetManagedObjects(crow::websocket::Connection& conn, |
| 246 | const boost::system::error_code& ec, |
| 247 | const dbus::utility::ManagedObjectType& objects) |
| 248 | { |
| 249 | const std::string* socketValue = nullptr; |
| 250 | const std::string* endpointValue = nullptr; |
| 251 | const std::string* endpointObjectPath = nullptr; |
| 252 | |
| 253 | if (ec) |
| 254 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 255 | BMCWEB_LOG_ERROR("DBus error: {}", ec.message()); |
Ed Tanous | 2da6b8c | 2023-02-27 14:25:10 -0800 | [diff] [blame] | 256 | conn.close("Failed to create mount point"); |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | for (const auto& [objectPath, interfaces] : objects) |
| 261 | { |
| 262 | for (const auto& [interface, properties] : interfaces) |
| 263 | { |
| 264 | if (interface != "xyz.openbmc_project.VirtualMedia.MountPoint") |
| 265 | { |
| 266 | continue; |
| 267 | } |
| 268 | |
| 269 | for (const auto& [name, value] : properties) |
| 270 | { |
| 271 | if (name == "EndpointId") |
| 272 | { |
| 273 | endpointValue = std::get_if<std::string>(&value); |
| 274 | |
| 275 | if (endpointValue == nullptr) |
| 276 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 277 | BMCWEB_LOG_ERROR("EndpointId property value is null"); |
Ed Tanous | 2da6b8c | 2023-02-27 14:25:10 -0800 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | if (name == "Socket") |
| 281 | { |
| 282 | socketValue = std::get_if<std::string>(&value); |
| 283 | if (socketValue == nullptr) |
| 284 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 285 | BMCWEB_LOG_ERROR("Socket property value is null"); |
Ed Tanous | 2da6b8c | 2023-02-27 14:25:10 -0800 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | if ((endpointValue != nullptr) && (socketValue != nullptr) && |
| 292 | *endpointValue == conn.req.target()) |
| 293 | { |
| 294 | endpointObjectPath = &objectPath.str; |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | if (objects.empty() || endpointObjectPath == nullptr) |
| 300 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 301 | BMCWEB_LOG_ERROR("Cannot find requested EndpointId"); |
Ed Tanous | 2da6b8c | 2023-02-27 14:25:10 -0800 | [diff] [blame] | 302 | conn.close("Failed to match EndpointId"); |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | for (const auto& session : sessions) |
| 307 | { |
| 308 | if (session.second->getEndpointId() == conn.req.target()) |
| 309 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 310 | BMCWEB_LOG_ERROR("Cannot open new connection - socket is in use"); |
Ed Tanous | 2da6b8c | 2023-02-27 14:25:10 -0800 | [diff] [blame] | 311 | conn.close("Slot is in use"); |
| 312 | return; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | // If the socket file exists (i.e. after bmcweb crash), |
| 317 | // we cannot reuse it. |
| 318 | std::remove((*socketValue).c_str()); |
| 319 | |
| 320 | sessions[&conn] = std::make_shared<NbdProxyServer>( |
| 321 | conn, *socketValue, *endpointValue, *endpointObjectPath); |
| 322 | |
| 323 | sessions[&conn]->run(); |
| 324 | }; |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 325 | inline void onOpen(crow::websocket::Connection& conn) |
| 326 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 327 | BMCWEB_LOG_DEBUG("nbd-proxy.onopen({})", logPtr(&conn)); |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 328 | |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 329 | sdbusplus::message::object_path path("/xyz/openbmc_project/VirtualMedia"); |
| 330 | dbus::utility::getManagedObjects( |
| 331 | "xyz.openbmc_project.VirtualMedia", path, |
Ed Tanous | 2da6b8c | 2023-02-27 14:25:10 -0800 | [diff] [blame] | 332 | [&conn](const boost::system::error_code& ec, |
Ed Tanous | e551b5f | 2023-02-27 14:19:07 -0800 | [diff] [blame] | 333 | const dbus::utility::ManagedObjectType& objects) { |
Ed Tanous | 2da6b8c | 2023-02-27 14:25:10 -0800 | [diff] [blame] | 334 | afterGetManagedObjects(conn, ec, objects); |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 335 | }); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 336 | |
| 337 | // We need to wait for dbus and the websockets to hook up before data is |
| 338 | // sent/received. Tell the core to hold off messages until the sockets are |
| 339 | // up |
| 340 | conn.deferRead(); |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | inline void onClose(crow::websocket::Connection& conn, |
| 344 | const std::string& reason) |
| 345 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 346 | BMCWEB_LOG_DEBUG("nbd-proxy.onclose(reason = '{}')", reason); |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 347 | auto session = sessions.find(&conn); |
| 348 | if (session == sessions.end()) |
| 349 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 350 | BMCWEB_LOG_DEBUG("No session to close"); |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 351 | return; |
| 352 | } |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 353 | // Remove reference to session in global map |
| 354 | sessions.erase(session); |
| 355 | } |
| 356 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 357 | inline void onMessage(crow::websocket::Connection& conn, std::string_view data, |
| 358 | crow::websocket::MessageType /*type*/, |
| 359 | std::function<void()>&& whenComplete) |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 360 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 361 | BMCWEB_LOG_DEBUG("nbd-proxy.onMessage(len = {})", data.size()); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 362 | |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 363 | // Acquire proxy from sessions |
| 364 | auto session = sessions.find(&conn); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 365 | if (session == sessions.end() || session->second == nullptr) |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 366 | { |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 367 | whenComplete(); |
| 368 | return; |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 369 | } |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 370 | |
| 371 | session->second->send(data, std::move(whenComplete)); |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 374 | inline void requestRoutes(App& app) |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 375 | { |
| 376 | BMCWEB_ROUTE(app, "/nbd/<str>") |
| 377 | .websocket() |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 378 | .onopen(onOpen) |
| 379 | .onclose(onClose) |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 380 | .onmessageex(onMessage); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 381 | } |
| 382 | } // namespace nbd_proxy |
| 383 | } // namespace crow |