28 #include <sys/types.h>
29 #include <arpa/nameser.h>
30 #include <netinet/in.h>
34 #include <arpa/nameser_compat.h>
37 #include <ndn-cxx/util/backports.hpp>
38 #include <ndn-cxx/util/exception.hpp>
42 #include <boost/endian/conversion.hpp>
46 using namespace std::string_literals;
51 uint8_t buf[NS_PACKETSZ];
59 parseSrvRr(
const QueryAnswer& queryAnswer,
int answerSize)
80 uint16_t ancount = queryAnswer.header.ancount;
81 if (boost::endian::big_to_native(ancount) == 0) {
82 NDN_THROW(
DnsSrvError(
"SRV record cannot be parsed"));
85 const uint8_t* blob = queryAnswer.buf + NS_HFIXEDSZ;
86 blob += dn_skipname(blob, queryAnswer.buf + answerSize) + NS_QFIXEDSZ;
88 char srvName[NS_MAXDNAME];
89 int serverNameSize = dn_expand(queryAnswer.buf,
90 queryAnswer.buf + answerSize,
94 if (serverNameSize <= 0) {
95 NDN_THROW(
DnsSrvError(
"SRV record cannot be parsed (error decoding domain name)"));
98 const srv_t* server =
reinterpret_cast<const srv_t*
>(&blob[
sizeof(rechdr)]);
99 uint16_t port = boost::endian::big_to_native(server->port);
101 blob += serverNameSize + NS_HFIXEDSZ + NS_QFIXEDSZ;
103 char hostName[NS_MAXDNAME];
104 int hostNameSize = dn_expand(queryAnswer.buf,
105 queryAnswer.buf + answerSize,
109 if (hostNameSize <= 0) {
110 NDN_THROW(
DnsSrvError(
"SRV record cannot be parsed (error decoding host name)"));
113 return "udp://"s + hostName +
":" + std::to_string(port);
119 std::string srvDomain =
"_ndn._udp." + fqdn;
120 std::cerr <<
"Sending DNS query for SRV record for " << srvDomain << std::endl;
128 QueryAnswer queryAnswer;
129 int answerSize = res_query(srvDomain.data(),
133 sizeof(queryAnswer));
134 if (answerSize == 0) {
135 NDN_THROW(
DnsSrvError(
"No DNS SRV records found for " + srvDomain));
146 std::cerr <<
"Sending DNS query for SRV record for _ndn._udp" << std::endl;
148 QueryAnswer queryAnswer;
156 int answerSize = res_search(
"_ndn._udp",
160 sizeof(queryAnswer));
162 if (answerSize == 0) {
163 NDN_THROW(
DnsSrvError(
"No DNS SRV records found for _ndn._udp"));
provide synchronous DNS SRV record querying
std::string querySrvRr(const std::string &fqdn)
Send DNS SRV request for fqdn.
std::string querySrvRrSearch()
Send DNS SRV request using search domain list.
static std::string parseSrvRr(const QueryAnswer &queryAnswer, int answerSize)
Parse SRV record.