mirror of
https://github.com/ZeroTier/ZeroTierOne
synced 2025-08-21 05:43:59 -07:00
Add DNS TXT resolver (need one for Windows)
This commit is contained in:
parent
01e8fd0b07
commit
846f03504e
3 changed files with 71 additions and 20 deletions
|
@ -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];
|
||||
|
|
|
@ -199,14 +199,12 @@ public:
|
|||
static int64_t getFileSize(const char *path);
|
||||
|
||||
/**
|
||||
* Get IP (v4 and/or v6) addresses for a given host
|
||||
*
|
||||
* This is a blocking resolver.
|
||||
*
|
||||
* @param name Host name
|
||||
* @return IP addresses in InetAddress sort order or empty vector if not found
|
||||
* Get full DNS TXT results
|
||||
*
|
||||
* @param name DNS FQDN
|
||||
* @return TXT record result(s) or empty on error or not found
|
||||
*/
|
||||
static std::vector<InetAddress> resolve(const char *name);
|
||||
static std::vector<std::string> resolveTxt(const char *name);
|
||||
|
||||
/**
|
||||
* @return Current time in milliseconds since epoch
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue