This commit is contained in:
folkert van heusden 2023-03-21 22:15:27 +01:00
parent 8278c8d308
commit 553688a983
Signed by untrusted user who does not match committer: folkert
GPG key ID: 6B6455EDFEED3BD1
3 changed files with 63 additions and 0 deletions

View file

@ -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

44
disk_backend_nbd.cpp Normal file
View file

@ -0,0 +1,44 @@
#include <fcntl.h>
#include <unistd.h>
#include "disk_backend_nbd.h"
#include "log.h"
#ifdef ESP32
#include <lwip/sockets.h>
#else
#include <sys/socket.h>
#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);
}

18
disk_backend_nbd.h Normal file
View file

@ -0,0 +1,18 @@
#include <string>
#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);
};