diff --git a/disk_backend_nbd.cpp b/disk_backend_nbd.cpp index 3d5d3e9..2ad6e30 100644 --- a/disk_backend_nbd.cpp +++ b/disk_backend_nbd.cpp @@ -12,16 +12,9 @@ disk_backend_nbd::disk_backend_nbd(const std::string & host, const int port) : - fd(socket(AF_INET, SOCK_STREAM, 0)) + host(host), + port(port) { - 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() @@ -29,6 +22,28 @@ disk_backend_nbd::~disk_backend_nbd() close(fd); } +bool disk_backend_nbd::begin() +{ + addrinfo *res = nullptr; + + addrinfo hints { 0 }; + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + + char port_str[8] { 0 }; + snprintf(port_str, sizeof port_str, "%d", port); + + int rc = getaddrinfo(host.c_str(), port_str, &hints, &res); + + if (rc != 0) { + DOLOG(ll_error, true, "disk_backend_nbd: cannot resolve \"%s\":%s: %s", host.c_str(), port_str, gai_strerror(rc)); + + return false; + } + + return true; +} + 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); diff --git a/disk_backend_nbd.h b/disk_backend_nbd.h index 0fe8678..a86b89f 100644 --- a/disk_backend_nbd.h +++ b/disk_backend_nbd.h @@ -1,4 +1,7 @@ +#include #include +#include +#include #include "disk_backend.h" @@ -6,13 +9,17 @@ class disk_backend_nbd : public disk_backend { private: - const int fd { -1 }; + const std::string host; + const int port; + 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 begin() override; - bool write(const off_t offset, const size_t n, const uint8_t *const from); + bool read(const off_t offset, const size_t n, uint8_t *const target) override; + + bool write(const off_t offset, const size_t n, const uint8_t *const from) override; };