28 #include <sys/types.h> 29 #include <netinet/in.h> 31 #include <arpa/nameser.h> 34 #include <arpa/nameser_compat.h> 41 namespace autoconfig {
46 uint8_t buf[NS_PACKETSZ];
54 parseSrvRr(
const QueryAnswer& queryAnswer,
int answerSize)
75 if (ntohs(queryAnswer.header.ancount) == 0) {
76 BOOST_THROW_EXCEPTION(
DnsSrvError(
"SRV record cannot be parsed"));
79 const uint8_t* blob = queryAnswer.buf + NS_HFIXEDSZ;
81 blob += dn_skipname(blob, queryAnswer.buf + answerSize) + NS_QFIXEDSZ;
83 char srvName[NS_MAXDNAME];
84 int serverNameSize = dn_expand(queryAnswer.buf,
85 queryAnswer.buf + answerSize,
89 if (serverNameSize <= 0) {
90 BOOST_THROW_EXCEPTION(
DnsSrvError(
"SRV record cannot be parsed (error decoding domain name)"));
93 const srv_t* server =
reinterpret_cast<const srv_t*
>(&blob[
sizeof(rechdr)]);
94 uint16_t convertedPort = be16toh(server->port);
96 blob += serverNameSize + NS_HFIXEDSZ + NS_QFIXEDSZ;
98 char hostName[NS_MAXDNAME];
99 int hostNameSize = dn_expand(queryAnswer.buf,
100 queryAnswer.buf + answerSize,
104 if (hostNameSize <= 0) {
105 BOOST_THROW_EXCEPTION(
DnsSrvError(
"SRV record cannot be parsed (error decoding host name)"));
108 std::string uri =
"udp://";
109 uri.append(hostName);
111 uri.append(to_string(convertedPort));
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 BOOST_THROW_EXCEPTION(
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 BOOST_THROW_EXCEPTION(
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