tools: rename autoconfig Base to Stage
Also, turn BaseDns into non-member functions.
refs #4158
Change-Id: Id3ab862497a07cc261ed34b2ab116f4014a4c6fa
diff --git a/tools/ndn-autoconfig/dns-srv.cpp b/tools/ndn-autoconfig/dns-srv.cpp
new file mode 100644
index 0000000..5bfa3d5
--- /dev/null
+++ b/tools/ndn-autoconfig/dns-srv.cpp
@@ -0,0 +1,169 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2017, Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "dns-srv.hpp"
+
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <resolv.h>
+#include <arpa/nameser.h>
+
+#ifdef __APPLE__
+#include <arpa/nameser_compat.h>
+#endif
+
+namespace ndn {
+namespace tools {
+namespace autoconfig {
+
+union QueryAnswer
+{
+ HEADER header;
+ uint8_t buf[NS_PACKETSZ];
+};
+
+/** \brief Parse SRV record
+ * \return FaceUri of the hub from the SRV record
+ * \throw DnsSrvError if SRV record cannot be parsed
+ */
+static std::string
+parseSrvRr(const QueryAnswer& queryAnswer, int answerSize)
+{
+ // The references of the next classes are:
+ // http://www.diablotin.com/librairie/networking/dnsbind/ch14_02.htm
+
+ struct rechdr
+ {
+ uint16_t type;
+ uint16_t iclass;
+ uint32_t ttl;
+ uint16_t length;
+ };
+
+ struct srv_t
+ {
+ uint16_t priority;
+ uint16_t weight;
+ uint16_t port;
+ uint8_t* target;
+ };
+
+ if (ntohs(queryAnswer.header.ancount) == 0) {
+ BOOST_THROW_EXCEPTION(DnsSrvError("SRV record cannot be parsed"));
+ }
+
+ const uint8_t* blob = queryAnswer.buf + NS_HFIXEDSZ;
+
+ blob += dn_skipname(blob, queryAnswer.buf + answerSize) + NS_QFIXEDSZ;
+
+ char srvName[NS_MAXDNAME];
+ int serverNameSize = dn_expand(queryAnswer.buf, // message pointer
+ queryAnswer.buf + answerSize, // end of message
+ blob, // compressed server name
+ srvName, // expanded server name
+ NS_MAXDNAME);
+ if (serverNameSize <= 0) {
+ BOOST_THROW_EXCEPTION(DnsSrvError("SRV record cannot be parsed (error decoding domain name)"));
+ }
+
+ const srv_t* server = reinterpret_cast<const srv_t*>(&blob[sizeof(rechdr)]);
+ uint16_t convertedPort = be16toh(server->port);
+
+ blob += serverNameSize + NS_HFIXEDSZ + NS_QFIXEDSZ;
+
+ char hostName[NS_MAXDNAME];
+ int hostNameSize = dn_expand(queryAnswer.buf, // message pointer
+ queryAnswer.buf + answerSize, // end of message
+ blob, // compressed host name
+ hostName, // expanded host name
+ NS_MAXDNAME);
+ if (hostNameSize <= 0) {
+ BOOST_THROW_EXCEPTION(DnsSrvError("SRV record cannot be parsed (error decoding host name)"));
+ }
+
+ std::string uri = "udp://";
+ uri.append(hostName);
+ uri.append(":");
+ uri.append(to_string(convertedPort));
+
+ return uri;
+}
+
+std::string
+querySrvRr(const std::string& fqdn)
+{
+ std::string srvDomain = "_ndn._udp." + fqdn;
+ std::cerr << "Sending DNS query for SRV record for " << srvDomain << std::endl;
+
+ res_init();
+
+ _res.retrans = 1;
+ _res.retry = 2;
+ _res.ndots = 10;
+
+ QueryAnswer queryAnswer;
+ int answerSize = res_query(srvDomain.data(),
+ ns_c_in,
+ ns_t_srv,
+ queryAnswer.buf,
+ sizeof(queryAnswer));
+ if (answerSize == 0) {
+ BOOST_THROW_EXCEPTION(DnsSrvError("No DNS SRV records found for " + srvDomain));
+ }
+ return parseSrvRr(queryAnswer, answerSize);
+}
+
+/**
+ * @brief Send DNS SRV request using search domain list
+ */
+std::string
+querySrvRrSearch()
+{
+ std::cerr << "Sending DNS query for SRV record for _ndn._udp" << std::endl;
+
+ QueryAnswer queryAnswer;
+
+ res_init();
+
+ _res.retrans = 1;
+ _res.retry = 2;
+ _res.ndots = 10;
+
+ int answerSize = res_search("_ndn._udp",
+ ns_c_in,
+ ns_t_srv,
+ queryAnswer.buf,
+ sizeof(queryAnswer));
+
+ if (answerSize == 0) {
+ BOOST_THROW_EXCEPTION(DnsSrvError("No DNS SRV records found for _ndn._udp"));
+ }
+
+ return parseSrvRr(queryAnswer, answerSize);
+}
+
+} // namespace autoconfig
+} // namespace tools
+} // namespace ndn