TeaSpeak-Client/native/dns/utils.h

33 lines
1.1 KiB
C
Raw Normal View History

2019-10-25 19:51:40 -04:00
#pragma once
#include <cstddef>
#include <string>
#include <functional>
#include <variant>
#include "./src/resolver.h"
namespace tc::dns {
struct ServerAddress {
std::string host;
uint16_t port;
};
typedef std::function<void(bool /* success */, const std::variant<std::string, ServerAddress>& /* data */)> cr_callback_t;
inline std::string domain_root(std::string input) {
size_t dot_count = 0;
auto it = input.rbegin();
while(it != input.rend()) {
if(*it == '.' && ++dot_count == 2)
break;
it++;
}
std::string result{};
result.append(input.begin() + std::distance(it, input.rend()), input.end());
return result;
}
extern void cr_ip(Resolver& resolver, const ServerAddress& address, const cr_callback_t& callback);
extern void cr_srv(Resolver& resolver, const ServerAddress& address, const cr_callback_t& callback, const std::string& application = "_ts._udp");
extern void cr_tsdns(Resolver& resolver, const ServerAddress& address, const cr_callback_t& callback);
extern void cr(Resolver& resolver, const tc::dns::ServerAddress& address, const tc::dns::cr_callback_t& callback);
}