28 #include <sys/types.h> 29 #include <arpa/nameser.h> 30 #include <netinet/in.h> 34 #include <arpa/nameser_compat.h> 39 #include <boost/endian/conversion.hpp> 43 namespace autoconfig {
50 uint8_t buf[NS_PACKETSZ];
58 parseSrvRr(
const QueryAnswer& queryAnswer,
int answerSize)
79 uint16_t ancount = queryAnswer.header.ancount;
80 if (boost::endian::big_to_native(ancount) == 0) {
81 NDN_THROW(
DnsSrvError(
"SRV record cannot be parsed"));
84 const uint8_t* blob = queryAnswer.buf + NS_HFIXEDSZ;
85 blob += dn_skipname(blob, queryAnswer.buf + answerSize) + NS_QFIXEDSZ;
87 char srvName[NS_MAXDNAME];
88 int serverNameSize = dn_expand(queryAnswer.buf,
89 queryAnswer.buf + answerSize,
93 if (serverNameSize <= 0) {
94 NDN_THROW(
DnsSrvError(
"SRV record cannot be parsed (error decoding domain name)"));
97 const srv_t* server =
reinterpret_cast<const srv_t*
>(&blob[
sizeof(rechdr)]);
98 uint16_t port = boost::endian::big_to_native(server->port);
100 blob += serverNameSize + NS_HFIXEDSZ + NS_QFIXEDSZ;
102 char hostName[NS_MAXDNAME];
103 int hostNameSize = dn_expand(queryAnswer.buf,
104 queryAnswer.buf + answerSize,
108 if (hostNameSize <= 0) {
109 NDN_THROW(
DnsSrvError(
"SRV record cannot be parsed (error decoding host name)"));
112 return "udp://"s + hostName +
":" + to_string(port);
118 std::string srvDomain =
"_ndn._udp." + fqdn;
119 std::cerr <<
"Sending DNS query for SRV record for " << srvDomain << std::endl;
127 QueryAnswer queryAnswer;
128 int answerSize = res_query(srvDomain.data(),
132 sizeof(queryAnswer));
133 if (answerSize == 0) {
134 NDN_THROW(
DnsSrvError(
"No DNS SRV records found for " + srvDomain));
145 std::cerr <<
"Sending DNS query for SRV record for _ndn._udp" << std::endl;
147 QueryAnswer queryAnswer;
155 int answerSize = res_search(
"_ndn._udp",
159 sizeof(queryAnswer));
161 if (answerSize == 0) {
162 NDN_THROW(
DnsSrvError(
"No DNS SRV records found for _ndn._udp"));
Copyright (c) 2014-2017, Regents of the University of California, Arizona Board of Regents...
provide synchronous DNS SRV record querying