This commit is contained in:
folkert van heusden 2023-03-22 07:33:08 +01:00
parent 52df10587d
commit f61d49c98f
Signed by untrusted user who does not match committer: folkert
GPG key ID: 6B6455EDFEED3BD1
4 changed files with 70 additions and 17 deletions

1
ESP32/disk_backend_nbd.cpp Symbolic link
View file

@ -0,0 +1 @@
../disk_backend_nbd.cpp

1
ESP32/disk_backend_nbd.h Symbolic link
View file

@ -0,0 +1 @@
../disk_backend_nbd.h

View file

@ -1,12 +1,15 @@
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include "disk_backend_nbd.h"
#include "log.h"
#ifdef ESP32
#include <lwip/netdb.h>
#include <lwip/sockets.h>
#else
#include <netdb.h>
#include <sys/socket.h>
#endif
@ -24,6 +27,20 @@ disk_backend_nbd::~disk_backend_nbd()
bool disk_backend_nbd::begin()
{
if (!connect(false)) {
DOLOG(ll_error, true, "disk_backend_nbd: cannot connect to NBD server");
return false;
}
DOLOG(info, true, "disk_backend_nbd: connected to NBD server");
return true;
}
bool disk_backend_nbd::connect(const bool retry)
{
do {
// LOOP until connected, logging message, exponential backoff?
addrinfo *res = nullptr;
addrinfo hints { 0 };
@ -36,18 +53,47 @@ bool disk_backend_nbd::begin()
int rc = getaddrinfo(host.c_str(), port_str, &hints, &res);
if (rc != 0) {
#ifdef ESP32
DOLOG(ll_error, true, "disk_backend_nbd: cannot resolve \"%s\":%s", host.c_str(), port_str);
#else
DOLOG(ll_error, true, "disk_backend_nbd: cannot resolve \"%s\":%s: %s", host.c_str(), port_str, gai_strerror(rc));
#endif
return false;
sleep(1);
continue;
}
return true;
for(addrinfo *p = res; p != NULL; p = p->ai_next) {
if ((fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
continue;
}
if (::connect(fd, p->ai_addr, p->ai_addrlen) == -1) {
close(fd);
fd = -1;
DOLOG(ll_error, true, "disk_backend_nbd: cannot connect");
continue;
}
break;
}
freeaddrinfo(res);
}
while(fd == -1 && retry);
return fd != -1;
}
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);
connect(true);
// TODO: loop dat als read() aangeeft dat de peer weg is, dat er dan gereconnect wordt
// anders return false
return pread(fd, target, n, offset) == ssize_t(n);
}
@ -55,5 +101,10 @@ bool disk_backend_nbd::write(const off_t offset, const size_t n, const uint8_t *
{
DOLOG(debug, false, "disk_backend_nbd::write: write %zu bytes to offset %zu", n, offset);
connect(true);
// TODO: loop dat als write() aangeeft dat de peer weg is, dat er dan gereconnect wordt
// anders return false
return pwrite(fd, from, n, offset) == ssize_t(n);
}

View file

@ -1,6 +1,4 @@
#include <netdb.h>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include "disk_backend.h"
@ -10,9 +8,11 @@ class disk_backend_nbd : public disk_backend
{
private:
const std::string host;
const int port;
const int port { 0 };
int fd { -1 };
bool connect(const bool retry);
public:
disk_backend_nbd(const std::string & host, const int port);
virtual ~disk_backend_nbd();