resolve hostname
This commit is contained in:
parent
cd55079217
commit
06d4373fc7
2 changed files with 34 additions and 12 deletions
|
@ -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);
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
#include <netdb.h>
|
||||
#include <string>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#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;
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue