From 553688a983877c20615a4255c1f4c60938b6f744 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Tue, 21 Mar 2023 22:15:27 +0100 Subject: [PATCH] wip --- CMakeLists.txt | 1 + disk_backend_nbd.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ disk_backend_nbd.h | 18 ++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 disk_backend_nbd.cpp create mode 100644 disk_backend_nbd.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e3bdc7c..55e709f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ add_executable( debugger.cpp disk_backend.cpp disk_backend_file.cpp + disk_backend_nbd.cpp error.cpp kw11-l.cpp loaders.cpp diff --git a/disk_backend_nbd.cpp b/disk_backend_nbd.cpp new file mode 100644 index 0000000..3d5d3e9 --- /dev/null +++ b/disk_backend_nbd.cpp @@ -0,0 +1,44 @@ +#include +#include + +#include "disk_backend_nbd.h" +#include "log.h" + +#ifdef ESP32 +#include +#else +#include +#endif + + +disk_backend_nbd::disk_backend_nbd(const std::string & host, const int port) : + fd(socket(AF_INET, SOCK_STREAM, 0)) +{ + struct addrinfo hints, *res; + int status; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + char *host = "esp-942daf.local"; + char *port = "5556"; + status = getaddrinfo(host, port, &hints, &res) +} + +disk_backend_nbd::~disk_backend_nbd() +{ + close(fd); +} + +bool disk_backend_nbd::read(const off_t offset, const size_t n, uint8_t *const target) +{ + DOLOG(debug, false, "disk_backend_nbd::read: read %zu bytes from offset %zu", n, offset); + + return pread(fd, target, n, offset) == ssize_t(n); +} + +bool disk_backend_nbd::write(const off_t offset, const size_t n, const uint8_t *const from) +{ + DOLOG(debug, false, "disk_backend_nbd::write: write %zu bytes to offset %zu", n, offset); + + return pwrite(fd, from, n, offset) == ssize_t(n); +} diff --git a/disk_backend_nbd.h b/disk_backend_nbd.h new file mode 100644 index 0000000..0fe8678 --- /dev/null +++ b/disk_backend_nbd.h @@ -0,0 +1,18 @@ +#include + +#include "disk_backend.h" + + +class disk_backend_nbd : public disk_backend +{ +private: + const int fd { -1 }; + +public: + disk_backend_nbd(const std::string & host, const int port); + virtual ~disk_backend_nbd(); + + bool read(const off_t offset, const size_t n, uint8_t *const target); + + bool write(const off_t offset, const size_t n, const uint8_t *const from); +};