malcontent/nss/resolver.hh

62 lines
1.4 KiB
C++
Raw Normal View History

2024-01-05 18:10:45 +01:00
// SPDX-FileCopyrightText: Matteo Settenvini <matteo.settenvini@montecristosoftware.eu>
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "wrapper.hh"
#include "cares_init.hh"
#include <ares.h>
#include <nss.h>
#include <memory>
#include <string>
#include <vector>
namespace malcontent {
struct ResolverArgs {
const char *name; // in
int family; // in
hostent *result; // out
char *buffer; // out
size_t buflen; // in
int *errnop; // out
HErrno *h_errnop; // out
int32_t *ttlp; // out
};
class Resolver {
public:
Resolver(std::vector<std::string> dns);
auto resolve(ResolverArgs& args) -> nss_status;
private:
struct CAresDeletor {
auto operator() (ares_channel_t *ch) const -> void {
ares_destroy(ch);
}
};
using CAresChannel = std::unique_ptr<ares_channel_t, CAresDeletor>;
static auto resolve_cb(void *arg,
int status,
int timeouts,
unsigned char *abuf,
int alen) -> void;
auto resolved(ResolverArgs& result,
int status,
int timeouts,
unsigned char *abuf,
int alen) -> nss_status;
std::shared_ptr<CAresLibrary> _ensure_cares_initialized;
CAresChannel _channel;
};
} // ~ namespace malcontent