Add DNS TXT resolver (need one for Windows)

This commit is contained in:
Adam Ierymenko 2019-08-16 16:49:30 -07:00
parent 01e8fd0b07
commit 846f03504e
No known key found for this signature in database
GPG key ID: 1657198823E52A61
3 changed files with 71 additions and 20 deletions

View file

@ -44,6 +44,14 @@
#include <sys/uio.h>
#include <dirent.h>
#include <netdb.h>
#include <arpa/nameser.h>
#include <resolv.h>
#ifndef C_IN
#define C_IN ns_c_in
#endif
#ifndef T_TXT
#define T_TXT ns_t_txt
#endif
#endif
#ifdef __WINDOWS__
@ -54,6 +62,8 @@
#include <iphlpapi.h>
#endif
#include <atomic>
#include "OSUtils.hpp"
namespace ZeroTier {
@ -303,6 +313,54 @@ int64_t OSUtils::getFileSize(const char *path)
return -1;
}
std::vector<std::string> OSUtils::resolveTxt(const char *host)
{
static std::atomic_bool libresolvInitialized(false);
if (!libresolvInitialized.exchange(true))
res_init();
std::vector<std::string> results;
uint8_t answer[32768];
char name[1024];
int alen = res_search(host,C_IN,T_TXT,answer,sizeof(answer));
if ((alen > 12)&&(alen < sizeof(answer))) {
uint8_t *pptr = answer + 12;
uint8_t *const end = answer + alen;
int explen = dn_expand(answer,end,pptr,name,sizeof(name));
if (explen > 0) {
pptr += explen;
if ((pptr + 2) >= end) return results;
unsigned int rtype = ((unsigned int)pptr[0] << 8) | (unsigned int)pptr[1];
if (rtype == T_TXT) {
pptr += 4;
if (pptr >= end) return results;
while (pptr < end) {
explen = dn_expand(answer,end,pptr,name,sizeof(name));
if (explen > 0) {
pptr += explen;
if ((pptr + 2) >= end) return results;
rtype = ((unsigned int)pptr[0] << 8) | (unsigned int)pptr[1];
pptr += 10;
if (pptr >= end) return results;
unsigned int elen = *(pptr++);
if (elen) {
if ((pptr + elen) > end) return results;
if (rtype == T_TXT)
results.push_back(std::string((const char *)pptr,elen));
pptr += elen;
}
} else {
return results;
}
}
}
}
}
return results;
}
bool OSUtils::readFile(const char *path,std::string &buf)
{
char tmp[16384];