wait to be verified
diff --git a/src/app/name-caching-resolver.cpp b/src/app/name-caching-resolver.cpp
new file mode 100644
index 0000000..728d0b0
--- /dev/null
+++ b/src/app/name-caching-resolver.cpp
@@ -0,0 +1,194 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, Regents of the University of California.
+ *
+ * This file is part of NDNS (Named Data Networking Domain Name Service).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include "name-caching-resolver.hpp"
+
+namespace ndn {
+namespace ndns {
+
+NameCachingResolver::NameCachingResolver(const char *programName, const char *prefix)
+: NDNApp(programName, prefix)
+//, m_resolverType(CachingResolver)
+{
+ this->setInterestLifetime(time::milliseconds(2000));
+}
+
+
+
+void
+NameCachingResolver::run()
+{
+ boost::asio::signal_set signalSet(m_ioService, SIGINT, SIGTERM);
+ signalSet.async_wait(boost::bind(&NDNApp::signalHandler, this));
+ // boost::bind(&NdnTlvPingServer::signalHandler, this)
+
+ Name name(m_prefix);
+ name.append(Query::toString(Query::QUERY_DNS_R));
+
+ m_face.setInterestFilter(name,
+ bind(&NameCachingResolver::onInterest,
+ this, _1, _2),
+ bind(&NDNApp::onRegisterFailed,
+ this, _1,_2));
+
+ std::cout << "\n=== NDNS Resolver "<<m_programName
+ <<" with routeble prefix "<< name.toUri()
+ << " starts===\n" << std::endl;
+
+ try {
+ m_face.processEvents();
+ }
+ catch (std::exception& e) {
+ std::cerr << "ERROR: " << e.what() << std::endl;
+ m_hasError = true;
+ m_ioService.stop();
+ }
+}
+
+
+
+void
+NameCachingResolver::onData(const Interest& interest, Data &data, IterativeQuery& iq)
+{
+ if (interest.getName() != iq.getLastInterest().getName())
+ {
+ std::cout<<iq<<std::endl;
+ std::cout<<"waiting for "<<iq.getLastInterest().getName().toUri()<<std::endl;
+ std::cout<<"coming data "<<data.getName().toUri()<<std::endl;
+ return;
+ }
+
+ iq.doData(data);
+
+ if (iq.getStep() == IterativeQuery::AnswerStub)
+ {
+ Data data = iq.getLastResponse().toData();
+ Name name = iq.getQuery().getAuthorityZone();
+ name.append(Query::toString(iq.getQuery().getQueryType()));
+ name.append(iq.getQuery().getRrLabel());
+ name.append(RR::toString(iq.getQuery().getRrType()));
+ name.appendVersion();
+ data.setName(name);
+ data.setFreshnessPeriod(iq.getLastResponse().getFreshness());
+
+ m_keyChain.sign(data);
+ m_face.put(data);
+ std::cout<<"[* <- *] answer Response ("
+ <<Response::toString(iq.getLastResponse().getResponseType())<<") to stub:"<<std::endl;
+ std::cout<<iq.getLastResponse()<<std::endl;
+ for (int i=0; i<15; i++)
+ {
+ std::cout<<"----";
+ }
+ std::cout<<std::endl<<std::endl;
+
+ } else if (iq.getStep() == IterativeQuery::NSQuery){
+ resolve(iq);
+ } else if (iq.getStep() == IterativeQuery::RRQuery) {
+ resolve(iq);
+ } else if (iq.getStep() == IterativeQuery::Abort) {
+ return;
+ } else {
+ std::cout<<"let me see the current step="<<IterativeQuery::toString(iq.getStep())<<std::endl;
+ std::cout<<iq<<std::endl;
+ }
+
+}
+
+
+void
+NameCachingResolver::answerRespNack(IterativeQuery& iq)
+{
+ Response re;
+
+ Name name = iq.getQuery().getAuthorityZone();
+ name.append(Query::toString(iq.getQuery().getQueryType()));
+ name.append(iq.getQuery().getRrLabel());
+ name.append(RR::toString(iq.getQuery().getRrType()));
+ name.appendVersion();
+
+ re.setResponseType(Response::NDNS_Nack);
+ re.setFreshness(this->getContentFreshness());
+ re.setQueryName(name);
+ Data data = re.toData();
+
+ m_keyChain.sign(data);
+ m_face.put(data);
+ std::cout<<"[* <- *] answer RRs to stub:"<<std::endl;
+ std::cout<<iq.getLastResponse().getStringRRs()<<std::endl;
+ for (int i=0; i<15; i++)
+ {
+ std::cout<<"----";
+ }
+ std::cout<<std::endl<<std::endl;
+}
+
+void
+NameCachingResolver::onInterest(const Name &name, const Interest &interest)
+{
+ Query query;
+ query.fromInterest(interest);
+ std::cout<<"[* -> *] receive Interest: "<<interest.getName().toUri()<<std::endl;
+ if (query.getQueryType() == Query::QUERY_DNS)
+ {
+ cout<<m_programName<<" is not in charge of Query_DNS"<<endl;
+ }
+
+ IterativeQuery iq(query);
+ //iq.setQuery(query);
+ resolve(iq);
+
+}
+
+
+void
+NameCachingResolver::onTimeout(const Interest& interest, IterativeQuery& iq)
+{
+ std::cout<<"[* !! *] timeout Interest "<<interest.getName().toUri()<<" timeouts"<<std::endl;
+
+ iq.doTimeout();
+ return;
+}
+
+void
+NameCachingResolver::resolve(IterativeQuery& iq) {
+
+ Interest interest = iq.toLatestInterest();
+
+ //must be set before express interest,since the call will back call iq
+ //if set after, then the iq in call of onData will return nothing
+ //be very careful here, as a new guy to c++
+
+ interest.setInterestLifetime(this->getInterestLifetime());
+ iq.setLastInterest(interest);
+ try {
+ m_face.expressInterest(interest,
+ boost::bind(&NameCachingResolver::onData, this, _1, _2, iq),
+ boost::bind(&NameCachingResolver::onTimeout, this, _1, iq)
+ );
+ }catch(std::exception& e) {
+ std::cerr << "ERROR: " << e.what() << std::endl;
+ }
+
+
+ std::cout<<"[* <- *] send Interest: "<<interest.getName().toUri()<<std::endl;
+ std::cout<<iq<<std::endl;
+}
+
+} /* namespace ndns */
+} /* namespace ndn */
diff --git a/src/app/name-caching-resolver.hpp b/src/app/name-caching-resolver.hpp
new file mode 100644
index 0000000..5f4fe53
--- /dev/null
+++ b/src/app/name-caching-resolver.hpp
@@ -0,0 +1,85 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, Regents of the University of California.
+ *
+ * This file is part of NDNS (Named Data Networking Domain Name Service).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef NAME_RESOLVER_HPP
+#define NAME_RESOLVER_HPP
+
+#include <boost/asio.hpp>
+#include <boost/noncopyable.hpp>
+
+#include <ndn-cxx/face.hpp>
+#include <ndn-cxx/name.hpp>
+#include <ndn-cxx/security/key-chain.hpp>
+
+#include "zone.hpp"
+#include "db/zone-mgr.hpp"
+#include "db/rr-mgr.hpp"
+#include "query.hpp"
+#include "response.hpp"
+#include "rr.hpp"
+#include "iterative-query.hpp"
+#include "ndn-app.hpp"
+
+using namespace std;
+
+namespace ndn {
+namespace ndns {
+
+
+class NameCachingResolver : public NDNApp{
+enum ResolverType
+{
+ StubResolver,
+ CachingResolver
+};
+
+public:
+
+NameCachingResolver(const char *programName, const char *prefix);
+
+
+void
+resolve(IterativeQuery& iq);
+
+void
+answerRespNack(IterativeQuery& iq);
+
+using NDNApp::onData;
+void
+onData(const Interest& interest, Data &data, IterativeQuery& iq);
+
+void
+onInterest(const Name &name, const Interest &interest);
+
+using NDNApp::onTimeout;
+
+void
+onTimeout(const Interest& interest, IterativeQuery& iq);
+
+void
+run();
+
+private:
+ //ResolverType m_resolverType;
+
+};
+
+} /* namespace ndns */
+} /* namespace ndn */
+
+#endif /* NAME_RESOLVER_HPP_ */
diff --git a/src/app/name-dig.cpp b/src/app/name-dig.cpp
new file mode 100644
index 0000000..6e64e31
--- /dev/null
+++ b/src/app/name-dig.cpp
@@ -0,0 +1,123 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, Regents of the University of California.
+ *
+ * This file is part of NDNS (Named Data Networking Domain Name Service).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include "name-dig.hpp"
+
+namespace ndn {
+namespace ndns {
+
+NameDig::NameDig(const char *programName, const char *prefix)
+: NDNApp(programName, prefix)
+, m_resolverName(Name("/"))
+, m_dstLabel(Name(prefix)){
+ //prefix in this app is the m_dstLabel
+ this->setInterestLifetime(time::milliseconds(10000));
+}
+
+NameDig::~NameDig() {
+ // TODO Auto-generated destructor stub
+}
+
+void
+NameDig::onData(const Interest& interest, Data& data)
+{
+ Response re;
+ re.fromData(data);
+ cout<<"get data:->"<<data.getName()<<endl;
+ cout<<"get response:->"<<re<<endl;
+
+
+
+ m_rrs = re.getRrs();
+
+
+
+ vector<RR>::iterator iter = m_rrs.begin();
+
+ while (iter != m_rrs.end())
+ {
+ RR rr = *iter;
+ cout<<rr<<endl;
+ iter ++;
+ }
+
+ this->stop();
+}
+
+void
+NameDig::sendQuery()
+{
+ Query q;
+ q.setAuthorityZone(this->m_resolverName);
+ q.setRrLabel(m_dstLabel);
+ q.setQueryType(Query::QUERY_DNS_R);
+ q.setRrType(m_rrType);
+
+ Interest interest = q.toInterest();
+ interest.setInterestLifetime(this->m_interestLifetime);
+ try {
+ m_face.expressInterest(interest,
+ boost::bind(&NameDig::onData, this, _1, _2),
+ boost::bind(&NameDig::onTimeout, this, _1)
+ );
+ std::cout<<"[* <- *] send Interest: "<<interest.getName().toUri()<<std::endl;
+ }catch(std::exception& e) {
+ m_hasError = true;
+ m_error = e.what();
+ }
+ m_interestTriedNum += 1;
+}
+
+void
+NameDig::onTimeout(const Interest& interest)
+{
+ std::cout<<"[* !! *] timeout Interest"<<interest.getName()<<std::endl;
+
+ if (m_interestTriedNum >= m_interestTriedMax)
+ {
+ m_error = "All Interests timeout";
+ m_hasError = true;
+ this->stop();
+ } else
+ {
+ sendQuery();
+ }
+
+}
+
+void
+NameDig::run()
+{
+
+ this->sendQuery();
+
+ try
+ {
+ m_face.processEvents();
+ }
+ catch (std::exception& e)
+ {
+ m_error = e.what();
+ m_hasError = true;
+ this->stop();
+ }
+
+}
+
+} /* namespace ndns */
+} /* namespace ndn */
diff --git a/src/app/name-dig.hpp b/src/app/name-dig.hpp
new file mode 100644
index 0000000..475cf26
--- /dev/null
+++ b/src/app/name-dig.hpp
@@ -0,0 +1,92 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, Regents of the University of California.
+ *
+ * This file is part of NDNS (Named Data Networking Domain Name Service).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef NAME_DIG_HPP_
+#define NAME_DIG_HPP_
+
+#include "ndn-app.hpp"
+#include "rr.hpp"
+#include "response.hpp"
+#include "query.hpp"
+
+namespace ndn {
+namespace ndns {
+
+class NameDig: public NDNApp {
+public:
+ NameDig(const char *programName, const char *prefix);
+ virtual ~NameDig();
+
+ void
+ onData(const ndn::Interest& interest, Data& data);
+
+
+ void
+ onTimeout(const ndn::Interest& interest);
+
+ void
+ sendQuery();
+
+ void
+ run();
+
+ const vector<RR>& getRrs() const {
+ return m_rrs;
+ }
+
+ void setRrs(const vector<RR>& rrs) {
+ m_rrs = rrs;
+ }
+
+ const Name& getResolverName() const {
+ return m_resolverName;
+ }
+
+ void setResolverName(const Name& resolverName) {
+ m_resolverName = resolverName;
+ }
+
+
+
+ const Name& getDstLabel() const {
+ return m_dstLabel;
+ }
+
+ void setDstLabel(const Name& dstLabel) {
+ m_dstLabel = dstLabel;
+ }
+
+ RR::RRType getRrType() const {
+ return m_rrType;
+ }
+
+ void setRrType(RR::RRType rrType) {
+ m_rrType = rrType;
+ }
+
+private:
+ Name m_resolverName;
+ Name m_dstLabel;
+ RR::RRType m_rrType;
+ vector<RR> m_rrs;
+};
+
+} /* namespace ndns */
+} /* namespace ndn */
+
+#endif /* NAME_DIG_HPP_ */
diff --git a/src/app/name-server.cpp b/src/app/name-server.cpp
new file mode 100644
index 0000000..6e6428d
--- /dev/null
+++ b/src/app/name-server.cpp
@@ -0,0 +1,143 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, Regents of the University of California.
+ *
+ * This file is part of NDNS (Named Data Networking Domain Name Service).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include "name-server.hpp"
+
+
+namespace ndn{
+namespace ndns{
+NameServer::NameServer(const char *programName, const char *prefix, const char *nameZone)
+: NDNApp(programName, prefix)
+, m_zone(Name(nameZone))
+, m_zoneMgr(m_zone)
+{
+ //m_zoneMgr.lookupId();
+}//NameServer Construction
+
+
+void
+NameServer::onInterest(const Name &name, const Interest &interest)
+{
+
+ cout<<"[* -> *] receive Interest: "<<interest.getName().toUri()<<std::endl;
+ Query query;
+ if (!query.fromInterest(interest))
+ {
+ cout<<"can resolve the Query from Interest: "<<endl;
+ return;
+ }
+
+ /*
+ * query.getAuthorityZone is routable name, not the zone's service name
+ if (query.getAuthorityZone() != m_zoneMgr.getZone().getAuthorizedName())
+ {
+ cout<<"Query is intent to zone: "<<query.getAuthorityZone()
+ <<". This is "<<m_zoneMgr.getZone().getAuthorizedName()<<endl;
+ return;
+ }
+ */
+
+ Response response;
+ Name name2 = interest.getName();
+ name2.appendVersion();
+ response.setQueryName(name2);
+ RRMgr mgr(m_zone, query, response);
+
+
+ if (mgr.lookup()<0)
+ {
+ cout<<"[* !! *] lookup error, then exit: "<<mgr.getErr()<<endl;
+ return;
+ }
+
+ if (response.getRrs().size() >0)
+ {
+ response.setResponseType(Response::NDNS_Resp);
+ } else {
+
+ if (query.getRrType() == RR::NS) {
+ int count = mgr.count();
+ if (count < 0)
+ {
+ cout<<"[* !! *] lookup error, then exit: "<<mgr.getErr()<<endl;
+ return;
+ } else if (count > 0)
+ {
+ response.setResponseType(Response::NDNS_Auth);
+ } else{
+ response.setResponseType(Response::NDNS_Nack);
+ }
+ } else {
+ response.setResponseType(Response::NDNS_Nack);
+ }
+ }
+
+
+ Data data = response.toData();
+ data.setFreshnessPeriod(response.getFreshness());
+
+ m_keyChain.sign(data);
+ m_face.put(data);
+ cout<<"[* <- *] send response: "<<response<<": "<<data<<endl;
+}//onInterest
+
+
+void
+NameServer::run()
+{
+ //m_zoneMgr.lookupId();
+ if (m_zoneMgr.getZone().getId() == 0)
+ {
+ m_hasError = true;
+ m_error = "cannot get Zone.id from database for name="+m_zone.getAuthorizedName().toUri();
+ stop();
+ }
+
+ boost::asio::signal_set signalSet(m_ioService, SIGINT, SIGTERM);
+ signalSet.async_wait(boost::bind(&NDNApp::signalHandler, this));
+ // boost::bind(&NdnTlvPingServer::signalHandler, this)
+ Name name;
+ name.set(m_prefix);
+ name.append(Query::toString(Query::QUERY_DNS));
+
+ m_face.setInterestFilter(name,
+ bind(&NameServer::onInterest,
+ this, _1, _2),
+ bind(&NDNApp::onRegisterFailed,
+ this, _1,_2));
+
+ std::cout << "\n=== NDNS Server for Zone "
+ << m_zoneMgr.getZone().getAuthorizedName().toUri()
+ <<" with routable prefix "<< name.toUri()
+ << " starts===\n" << std::endl;
+
+ try {
+ m_face.processEvents();
+ }
+ catch (std::exception& e) {
+ m_hasError = true;
+ m_error = "ERROR: ";
+ m_error += e.what();
+ stop();
+ }
+
+}//run
+
+} //namespace ndns
+} /* namespace ndn */
+
diff --git a/src/app/name-server.hpp b/src/app/name-server.hpp
new file mode 100644
index 0000000..9d6f5cd
--- /dev/null
+++ b/src/app/name-server.hpp
@@ -0,0 +1,77 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, Regents of the University of California.
+ *
+ * This file is part of NDNS (Named Data Networking Domain Name Service).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef NDNS_NAME_SERVER_HPP
+#define NDNS_NAME_SERVER_HPP
+
+#include <boost/asio.hpp>
+#include <boost/noncopyable.hpp>
+
+#include <ndn-cxx/face.hpp>
+#include <ndn-cxx/name.hpp>
+#include <ndn-cxx/security/key-chain.hpp>
+
+#include "zone.hpp"
+#include "db/zone-mgr.hpp"
+#include "db/rr-mgr.hpp"
+#include "query.hpp"
+#include "response.hpp"
+#include "rr.hpp"
+
+#include "ndn-app.hpp"
+
+using namespace std;
+using namespace ndn;
+
+
+namespace ndn{
+namespace ndns{
+class NameServer : public NDNApp
+{
+
+public:
+explicit
+NameServer(const char *programName, const char *prefix, const char *nameZone);
+
+void
+onInterest(const Name &name, const Interest &interest);
+
+
+void
+run();
+
+public:
+ /*
+ * the name used by the server to provide routeable accessory.
+
+ Name m_name;
+ */
+ /*
+ * the zone the server is in charge of
+ */
+ Zone m_zone;
+
+ /*
+ * to manage the m_zone; m_zoneMgr.getZone() returns the referrence of m_zone
+ */
+ ZoneMgr m_zoneMgr;
+
+};//clcass NameServer
+}//namespace ndns
+}//namespace ndn
+#endif /* NAME_SERVER_HPP_ */
diff --git a/src/resolver.cpp b/src/app/ndn-app.cpp
similarity index 69%
rename from src/resolver.cpp
rename to src/app/ndn-app.cpp
index fb9dd13..b8f123a 100644
--- a/src/resolver.cpp
+++ b/src/app/ndn-app.cpp
@@ -16,30 +16,32 @@
* You should have received a copy of the GNU General Public License along with
* NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
-
-#include "resolver.hpp"
+#include "ndn-app.hpp"
namespace ndn {
namespace ndns {
-Resolver::Resolver()
-{
-
-}
-
-Resolver::~Resolver()
+NDNApp::NDNApp(const char *programName, const char *prefix)
+ : m_programName(programName)
+ , m_prefix(prefix)
+ , m_hasError(false)
+ , m_contentFreshness(time::milliseconds(40000))
+ , m_interestLifetime(time::milliseconds(4000))
+ , m_interestTriedMax(2)
+ , m_interestTriedNum(0)
+ , m_face(m_ioService)
{
}
-const RR Resolver::iterativelyResolve(const std::string& domain, const std::string& name)
+NDNApp::~NDNApp()
{
- return "ex";
+ m_programName = 0;
+ m_prefix = 0;
}
-const RR Resolver::recusivelyResolve(const std::string& domain, const std::string& name)
-{
- return "ex";
-}
-} // namespace ndns
-} // namespace ndn
+
+
+
+} //namespace ndns
+} /* namespace ndn */
diff --git a/src/app/ndn-app.hpp b/src/app/ndn-app.hpp
new file mode 100644
index 0000000..00595ea
--- /dev/null
+++ b/src/app/ndn-app.hpp
@@ -0,0 +1,170 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, Regents of the University of California.
+ *
+ * This file is part of NDNS (Named Data Networking Domain Name Service).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef NDNS_NDN_APP_HPP
+#define NDNS_NDN_APP_HPP
+
+#include <ndn-cxx/face.hpp>
+#include <ndn-cxx/name.hpp>
+#include <ndn-cxx/security/key-chain.hpp>
+
+#include <boost/asio.hpp>
+#include <boost/bind.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
+#include <boost/noncopyable.hpp>
+
+using namespace std;
+
+namespace ndn {
+namespace ndns{
+
+class NDNApp {
+public:
+ NDNApp(const char *programName, const char *prefix);
+ virtual ~NDNApp();
+
+ virtual void
+ onData(const ndn::Interest& interest, Data& data)
+ {
+ }
+ virtual void
+ onTimeout(const ndn::Interest& interest)
+ {
+ std::cout<<"!- Interest Timeout"<<interest.getName()<<std::endl;
+ }
+
+ virtual void
+ onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
+ {
+ m_error = "ERROR: Failed to register prefix in local hub's daemon";
+ m_error += " due to: ";
+ m_error += reason;
+ m_hasError = true;
+ this->stop();
+ }
+
+
+ virtual void
+ onInterest(const Name &name, const Interest& interest)
+ {
+
+ }
+
+ virtual void
+ signalHandler()
+ {
+ this->stop();
+ exit(1);
+ }
+
+
+ virtual void
+ stop()
+ {
+ std::cout<<m_programName<<" stops"<<std::endl;
+ m_ioService.stop();
+ m_face.shutdown();
+ if (hasError())
+ {
+ cout<<m_error<<endl;
+ }
+ }
+
+
+
+ bool hasError() const {
+ return m_hasError;
+ }
+
+ void setHasError(bool hasError) {
+ m_hasError = hasError;
+ }
+
+ const char* getPrefix() const {
+ return m_prefix;
+ }
+
+ void setPrefix(char* prefix) {
+ m_prefix = prefix;
+ }
+
+ const char* getProgramName() const {
+ return m_programName;
+ }
+
+ void setProgramName(char* programName) {
+ m_programName = programName;
+ }
+
+ const string& getErr() const {
+ return m_error;
+ }
+
+ void setErr(const string& err) {
+ m_error = err;
+ }
+
+ uint32_t getInterestTriedMax() const {
+ return m_interestTriedMax;
+ }
+
+ void setInterestTriedMax(uint32_t interestTriedMax) {
+ m_interestTriedMax = interestTriedMax;
+ }
+
+ time::milliseconds getContentFreshness() const {
+ return m_contentFreshness;
+ }
+
+ void setContentFreshness(time::milliseconds contentFreshness) {
+ m_contentFreshness = contentFreshness;
+ }
+
+ time::milliseconds getInterestLifetime() const {
+ return m_interestLifetime;
+ }
+
+ void setInterestLifetime(time::milliseconds interestLifetime) {
+ m_interestLifetime = interestLifetime;
+ }
+
+public:
+ const char* m_programName;
+ const char* m_prefix;
+ bool m_hasError;
+ string m_error;
+ time::milliseconds m_contentFreshness;
+ time::milliseconds m_interestLifetime;
+
+ uint32_t m_interestTriedMax;
+ uint32_t m_interestTriedNum;
+
+ //Name m_name;
+ boost::asio::io_service m_ioService;
+ //Zone m_zone;
+ //ZoneMgr m_zoneMgr;
+
+
+ Face m_face;
+ KeyChain m_keyChain;
+};
+
+} //namespace ndns
+} /* namespace ndn */
+
+#endif /* NDN_APP_HPP_ */
diff --git a/src/db/README.txt b/src/db/README.txt
new file mode 100644
index 0000000..ea73d04
--- /dev/null
+++ b/src/db/README.txt
@@ -0,0 +1,11 @@
+#how to create the database
+pre-requirements:
+1. sqlite3 installed
+
+Steps to install and reset the database:
+1. run shell and go to the src/db directory as working directory
+2. run command sqlite3 ndns-local.db on shell and thus, enter the sqlit3 shell program
+3. run .read ndns-database-tables.sql, which create the tables
+4. run .read ndns-database-data-demo.sql, which insert demo data into database
+
+
diff --git a/src/db/db-mgr.cpp b/src/db/db-mgr.cpp
new file mode 100644
index 0000000..5ab9db1
--- /dev/null
+++ b/src/db/db-mgr.cpp
@@ -0,0 +1,103 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, Regents of the University of California.
+ *
+ * This file is part of NDNS (Named Data Networking Domain Name Service).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <iostream>
+#include <fstream>
+
+#include "db-mgr.hpp"
+
+namespace ndn {
+namespace ndns {
+
+DBMgr::DBMgr()
+: m_dbfile("src/db/ndns-local.db")
+, m_conn(0)
+, m_reCode(-1)
+, m_status(DBClosed)
+, m_resultNum(0)
+{
+ std::fstream file;
+ file.open(m_dbfile,std::ios::in);
+ if (file)
+ {
+ std::cout<<"database file "<<m_dbfile<<" does exist"<<std::endl;
+ } else
+ {
+ std::cout<<"database file "<<m_dbfile<<" does not exist"<<std::endl;
+ }
+
+}
+
+
+DBMgr::~DBMgr()
+{
+}
+
+void DBMgr::open()
+{
+ if (m_status == DBConnected)
+ return;
+
+ m_reCode = sqlite3_open(this->m_dbfile.c_str(), &(this->m_conn));
+ if (m_reCode != SQLITE_OK)
+ {
+ m_err = "Cannot connect to the db: "+this->m_dbfile;
+ m_status = DBError;
+ //exit(1);
+ }else
+ {
+ std::cout<<"connect to the db: "<<m_dbfile<<std::endl;
+ }
+ m_status = DBConnected;
+}
+
+void DBMgr::close()
+{
+ if (m_status == DBClosed)
+ return;
+
+ m_reCode = sqlite3_close(this->m_conn);
+ if (m_reCode != SQLITE_OK)
+ {
+ m_err = "Cannot close the db: "+this->m_dbfile;
+ m_status = DBError;
+ }
+ m_status = DBClosed;
+}
+
+void DBMgr::execute(std::string sql, int (*callback)(void*,int,char**,char**), void * paras)
+{
+ if (m_status == DBClosed)
+ this->open();
+
+ clearResultNum();
+ char *err_msg;
+ m_reCode = sqlite3_exec(m_conn, sql.c_str(), callback, paras, &err_msg);
+ if (m_reCode != SQLITE_OK)
+ {
+ m_status = DBError;
+ this->m_err.append(err_msg);
+ std::cout<<this->m_err<<std::endl;
+ }
+ this->close();
+}
+
+}//namespace ndns
+}//namespace ndn
+
diff --git a/src/db/db-mgr.hpp b/src/db/db-mgr.hpp
new file mode 100644
index 0000000..c6445af
--- /dev/null
+++ b/src/db/db-mgr.hpp
@@ -0,0 +1,109 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, Regents of the University of California.
+ *
+ * This file is part of NDNS (Named Data Networking Domain Name Service).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NDNS_DB_MGR_CPP
+#define NDNS_DB_MGR_CPP
+
+#include <sqlite3.h>
+#include <iostream>
+
+
+namespace ndn {
+namespace ndns {
+
+
+class DBMgr
+{
+public:
+ enum m_status
+ {
+ DBConnected,
+ DBClosed,
+ DBError
+ };
+
+public:
+ DBMgr();
+ virtual ~DBMgr();
+
+ void open();
+ void close();
+ void execute(std::string sql,
+ int (*callback)(void*,int,char**,char**), void * paras);
+
+ inline void addResultNum(){
+ m_resultNum += 1;
+ }
+ inline void clearResultNum() {
+ m_resultNum = 0;
+ }
+
+ const std::string& getDbfile() const {
+ return m_dbfile;
+ }
+
+ void setDbfile(const std::string& dbfile) {
+ this->m_dbfile = dbfile;
+ }
+
+ const std::string& getErr() const {
+ return m_err;
+ }
+
+ void setErr(const std::string& err) {
+ this->m_err = err;
+ }
+
+ int getReCode() const {
+ return m_reCode;
+ }
+
+ void setReCode(int reCode) {
+ this->m_reCode = reCode;
+ }
+
+ m_status getStatus() const {
+ return m_status;
+ }
+
+ void setStatus(m_status status) {
+ this->m_status = status;
+ }
+
+ int getResultNum() const {
+ return m_resultNum;
+ }
+
+ void setResultNum(int resultNum) {
+ m_resultNum = resultNum;
+ }
+
+
+private:
+ std::string m_dbfile;
+ sqlite3 *m_conn;
+ std::string m_err;
+ int m_reCode;
+ m_status m_status;
+ int m_resultNum;
+}; //class DBMgr
+}//namespace ndns
+}//namespace ndn
+
+#endif
diff --git a/src/db/ndns-db-data-demo.sql b/src/db/ndns-db-data-demo.sql
new file mode 100644
index 0000000..8564207
--- /dev/null
+++ b/src/db/ndns-db-data-demo.sql
@@ -0,0 +1,40 @@
+
+delete from rrs;
+delete from rrsets;
+delete from zones;
+
+insert into zones (id, name) values
+ (1, "/"),
+ (2, "/net"),
+ (3, "/net/ndnsim"),
+ (4, "/dns/google/com"),
+ (5, "/net/ndnsim/git/doc")
+ ;
+
+insert into rrsets (id, zone_id, label, class, type, ndndata) values
+ (1, 3, "/www", NULL, "TXT", NULL),
+ (2, 3, "/doc", NULL, "TXT", NULL),
+ (3, 2, "/ndnsim", NULL, "NS", NULL),
+ (4, 1, "/net", NULL, "NS", NULL),
+ (5, 4, "/net", NULL, "NS", NULL),
+ (6, 3, "/git/www", NULL, "TXT", NULL),
+ (7, 3, "/git/doc", NULL, "NS", NULL),
+ (8, 5, "/www", NULL, "TXT", NULL),
+ (9, 3, "/repo/www", NULL, "TXT", NULL),
+ (10, 3, "/norrdata", NULL, "TXT", NULL)
+ ;
+
+
+insert into rrs (id, rrset_id, ttl, rrdata) values
+ (1, 1, 3600, "/ucla"),
+ (2, 1, 8000, "/att"),
+ (3, 2, 3600, "/ucla"),
+ (4, 3, 4000, "/net/ndnsim2"),
+ (5, 4, 5000, "/net"),
+ (6, 5, 6000, "/net"),
+ (7, 3, 4000, "/net/ndnsim"),
+ (8, 6, 3000, "/net/ndnsim/git/www"),
+ (9, 7, 3000, "/net/ndnsim/git/doc"),
+ (10,8, 3000, "/net/ndnsim/git/doc/www"),
+ (11,9, 3000, "/net/ndnsim/repo/www")
+ ;
diff --git a/src/db/ndns-db-tables.sql b/src/db/ndns-db-tables.sql
new file mode 100644
index 0000000..8ecc3b0
--- /dev/null
+++ b/src/db/ndns-db-tables.sql
@@ -0,0 +1,39 @@
+/*
+some difference with Alex's sql file:
+1. class field in table rrsets could be NULL
+2. type field in table rrsets is changed to text from integer(10)
+*/
+DROP TRIGGER IF EXISTS rrs_update;
+DROP TABLE IF EXISTS zones;
+DROP TABLE IF EXISTS rrsets;
+DROP TABLE IF EXISTS rrs;
+CREATE TABLE zones (
+ id INTEGER NOT NULL PRIMARY KEY,
+ name blob NOT NULL UNIQUE);
+CREATE TABLE rrsets (
+ id INTEGER NOT NULL PRIMARY KEY,
+ zone_id integer(10) NOT NULL,
+ label text NOT NULL,
+ class integer(10),
+ type text NOT NULL,
+ ndndata blob,
+ FOREIGN KEY(zone_id) REFERENCES zones(id) ON UPDATE Cascade ON DELETE Cascade);
+CREATE TABLE rrs (
+ id INTEGER NOT NULL PRIMARY KEY,
+ rrset_id integer(10) NOT NULL,
+ ttl integer(10) NOT NULL,
+ rrdata blob NOT NULL,
+ FOREIGN KEY(rrset_id) REFERENCES rrsets(id) ON UPDATE Cascade ON DELETE Cascade);
+CREATE UNIQUE INDEX rrsets_zone_id_label_class_type
+ ON rrsets (zone_id, label, class, type);
+CREATE INDEX rrs_rrset_id
+ ON rrs (rrset_id);
+CREATE INDEX rrs_rrset_id_rrdata
+ ON rrs (rrset_id, rrdata);
+CREATE TRIGGER rrs_update
+BEFORE INSERT ON rrs
+FOR EACH ROW
+BEGIN
+ DELETE FROM rrs WHERE rrset_id = NEW.rrset_id AND rrdata = NEW.rrdata;
+END;
+
diff --git a/src/db/ndns-local.db b/src/db/ndns-local.db
new file mode 100644
index 0000000..a9177ca
--- /dev/null
+++ b/src/db/ndns-local.db
Binary files differ
diff --git a/src/db/rr-mgr.cpp b/src/db/rr-mgr.cpp
new file mode 100644
index 0000000..51e0e3b
--- /dev/null
+++ b/src/db/rr-mgr.cpp
@@ -0,0 +1,112 @@
+/*
+ * rr-mgr.cpp
+ *
+ * Created on: 23 Jul, 2014
+ * Author: shock
+ */
+
+#include "rr-mgr.hpp"
+
+namespace ndn {
+namespace ndns{
+RRMgr::RRMgr(Zone& zone, Query& query, Response& response)
+ : m_count(0U)
+ , m_zone(zone)
+ , m_query(query)
+ , m_response(response)
+{
+
+}
+
+
+RRMgr::~RRMgr() {
+ // TODO Auto-generated destructor stub
+}
+
+
+
+int
+RRMgr::count()
+{
+ std::string sql;
+ sql = "SELECT count(*) FROM rrs INNER JOIN rrsets ON rrs.rrset_id=rrsets.id";
+ sql += " WHERE rrsets.zone_id=";
+ sql += std::to_string(m_zone.getId());
+ sql += " AND ";
+ sql += "rrsets.type=\'";
+ sql += RR::toString(m_query.getRrType());
+ sql += "\' AND ";
+ sql += "rrsets.label LIKE\'";
+ sql += m_query.getRrLabel().toUri()+"/%\'";
+
+ std::cout<<"sql="<<sql<<std::endl;
+ this->execute(sql, static_callback_countRr, this);
+
+ if (this->getStatus() == DBMgr::DBError)
+ {
+ return -1;
+ }
+
+ return this->m_count;
+}
+int
+RRMgr::callback_countRr(int argc, char **argv, char **azColName)
+{
+ this->addResultNum();
+
+ std::cout<<this->getResultNum()<<"th result: "<<"count="<<argv[0]<<std::endl;
+ m_count = std::atoi(argv[0]);
+ return 0;
+}
+
+int RRMgr::lookup()
+{
+ std::string sql;
+
+ sql = "SELECT rrs.ttl, rrs.rrdata, rrs.id FROM rrs INNER JOIN rrsets ON rrs.rrset_id=rrsets.id";
+ sql += " WHERE rrsets.zone_id=";
+ sql += std::to_string(m_zone.getId());
+ sql += " AND ";
+ sql += "rrsets.type=\'";
+ sql += RR::toString(m_query.getRrType());
+ sql += "\' AND ";
+ sql += "rrsets.label=\'";
+ sql += m_query.getRrLabel().toUri()+"\'";
+ sql += " ORDER BY rrs.id";
+
+ std::cout<<"sql="<<sql<<std::endl;
+ this->execute(sql, static_callback_getRr, this);
+
+ if (this->getStatus() == DBMgr::DBError)
+ {
+ return -1;
+ }
+
+ //m_response.setQueryName(m_query.getAuthorityZone());
+ return 0;
+}
+
+int RRMgr::callback_getRr(int argc, char **argv, char **azColName)
+{
+ this->addResultNum();
+ if (argc < 1)
+ {
+ this->setErr("No RRType="+RR::toString(m_query.getRrType())+
+ " and label="+m_query.getRrLabel().toUri()+
+ " and zone="+m_zone.getAuthorizedName().toUri());
+ return 0;
+ }
+
+ std::cout<<this->getResultNum()<<"th result: "<<"id="<<argv[2]
+ <<" ttl="<<argv[0]<<" rrdata="<<argv[1]<<std::endl;
+
+ m_response.setFreshness(time::milliseconds(std::atoi(argv[0])));
+
+ m_response.addRr(std::atoi(argv[2]), argv[1]);
+ //std::cout<<"after add a Rr: current size="<<m_response.getRrs().size()<<std::endl;
+ //m_response.setFreshness(time::milliseconds(std::atoi(argv[0])));
+ return 0;
+}
+
+}//namespace ndnsn
+} /* namespace ndn */
diff --git a/src/db/rr-mgr.hpp b/src/db/rr-mgr.hpp
new file mode 100644
index 0000000..e940d10
--- /dev/null
+++ b/src/db/rr-mgr.hpp
@@ -0,0 +1,94 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, Regents of the University of California.
+ *
+ * This file is part of NDNS (Named Data Networking Domain Name Service).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef RR_MGR_HPP
+#define RR_MGR_HPP
+
+#include "db-mgr.hpp"
+#include "query.hpp"
+#include "response.hpp"
+#include "zone.hpp"
+
+
+namespace ndn {
+namespace ndns{
+class RRMgr : public DBMgr {
+public:
+ RRMgr( Zone& zone, Query& query, Response& response);
+ virtual ~RRMgr();
+
+public:
+ int lookup();
+
+ int callback_getRr(int argc, char **argv, char **azColName);
+
+ static int
+ static_callback_getRr(void *param, int argc, char **argv, char **azColName)
+ {
+ RRMgr *mgr = reinterpret_cast<RRMgr*>(param);
+ return mgr->callback_getRr(argc, argv, azColName);
+ }
+
+ int count();
+ int callback_countRr(int argc, char **argv, char **azColName);
+
+ static int
+ static_callback_countRr(void *param, int argc, char **argv, char **azColName)
+ {
+ RRMgr *mgr = reinterpret_cast<RRMgr*>(param);
+ return mgr->callback_countRr(argc, argv, azColName);
+ }
+
+ const Query& getQuery() const {
+ return m_query;
+ }
+
+ void setQuery(const Query& query) {
+ m_query = query;
+ }
+
+ const Response& getResponse() const {
+ return m_response;
+ }
+
+ void setResponse(const Response& response) {
+ m_response = response;
+ }
+
+ const Zone& getZone() const {
+ return m_zone;
+ }
+
+ void setZone(const Zone& zone) {
+ m_zone = zone;
+ }
+
+private:
+ unsigned int m_count;
+ Zone& m_zone;
+ Query& m_query;
+ Response& m_response;
+};
+
+
+
+} //namespace ndns
+} /* namespace ndn */
+
+#endif /* RR_MGR_HPP_ */
diff --git a/src/db/zone-mgr.cpp b/src/db/zone-mgr.cpp
new file mode 100644
index 0000000..cc0b613
--- /dev/null
+++ b/src/db/zone-mgr.cpp
@@ -0,0 +1,77 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, Regents of the University of California.
+ *
+ * This file is part of NDNS (Named Data Networking Domain Name Service).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "zone-mgr.hpp"
+
+
+namespace ndn {
+namespace ndns {
+
+ZoneMgr::ZoneMgr( Zone& zone)
+: m_zone(zone)
+{
+ this->lookupId();
+}
+
+void
+ZoneMgr::lookupId(const Name& name)
+{
+ this->open();
+ std::string sql = "SELECT id FROM zones WHERE name=\'"+name.toUri()+"\'";
+ //sql = "SELECT * FROM ZONES";
+ std::cout<<"sql="<<sql<<std::endl;
+ //std::cout<<"*this="<<this<<" m_zone.id="<<m_zone.getId()<<" zoneId="<<&m_zone<<std::endl;
+ this->execute(sql, static_callback_setId, this);
+ //std::cout<<"*this="<<this<<" m_zone.id="<<m_zone.getId()<<" zoneId="<<&m_zone<<std::endl;
+ this->close();
+
+}
+int
+ZoneMgr::callback_setId(int argc, char **argv, char **azColName)
+{
+ //Zone zone = this->getZone();
+ this->addResultNum();
+
+ Zone zone = this->m_zone;
+
+ if (argc < 1)
+ {
+ this->setErr("No Zone with Name "+zone.getAuthorizedName().toUri());
+ return -1;
+ }
+ //std::cout<<"id="<<(uint32_t)std::atoi(argv[0])<<" "<<std::endl;
+ int t1 = std::atoi(argv[0]);
+
+
+ m_zone.setId(t1);
+
+ return 0;
+}
+void
+ZoneMgr::lookupId()
+{
+ lookupId(this->m_zone.getAuthorizedName());
+}
+
+
+}//namepsace ndns
+}//namespace ndn
+
+
+
diff --git a/src/db/zone-mgr.hpp b/src/db/zone-mgr.hpp
new file mode 100644
index 0000000..8ced971
--- /dev/null
+++ b/src/db/zone-mgr.hpp
@@ -0,0 +1,72 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, Regents of the University of California.
+ *
+ * This file is part of NDNS (Named Data Networking Domain Name Service).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NDNS_ZONE_MGR_CPP
+#define NDNS_ZONE_MGR_CPP
+
+#include <sqlite3.h>
+
+#include "db-mgr.hpp"
+#include "zone.hpp"
+#include <ndn-cxx/name.hpp>
+
+namespace ndn {
+namespace ndns {
+
+class ZoneMgr : public DBMgr
+{
+
+public:
+ ZoneMgr( Zone& zone);
+
+public:
+ void
+ lookupId(const Name& name);
+
+ void
+ lookupId();
+
+ const Zone& getZone() const {
+ return m_zone;
+ }
+
+ void setZone(const Zone& zone) {
+ this->m_zone = zone;
+ }
+
+ int
+ callback_setId(int argc, char **argv, char **azColName);
+
+
+ static int
+ static_callback_setId(void *param, int argc, char **argv, char **azColName){
+
+ ZoneMgr *mgr = reinterpret_cast<ZoneMgr*>(param);
+ return mgr->callback_setId(argc, argv, azColName);
+ }
+
+private:
+ Zone& m_zone;
+};//class ZoneMgr
+
+
+
+}//namespace ndns
+}//namespace ndn
+#endif
diff --git a/src/ndns-tlv.hpp b/src/ndns-tlv.hpp
index 5343a96..b027f1a 100644
--- a/src/ndns-tlv.hpp
+++ b/src/ndns-tlv.hpp
@@ -17,18 +17,29 @@
* NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
+#ifndef NDNS_NDNS_TLV_HPP
+#define NDNS_NDNS_TLV_HPP
namespace ndn {
namespace ndns {
namespace tlv {
enum {
- Type = 2000,
- Fressness = 2001,
- ContentBlob = 2002,
- NumberOfRRData = 2003,
- RRData = 2004
+ ResponseType = 130,
+ ResponseFressness = 131,
+ ResponseContentBlob = 132,
+ ResponseNumberOfRRData = 133,
+
+ RRData = 134,
+ RRDataSub1 = 141,
+ RRDataSub2 = 142,
+
+ NDNSRecusive = 151,
+ NDNSInteative = 152
+
};
} // namespace tlv
} // namespace ndns
} // namespace ndn
+
+#endif
diff --git a/src/query.cpp b/src/query.cpp
index 756de40..7a96731 100644
--- a/src/query.cpp
+++ b/src/query.cpp
@@ -23,7 +23,9 @@
namespace ndns {
Query::Query()
- : m_interestLifetime(time::milliseconds(4000))
+ : m_queryType (QUERY_DNS)
+ , m_interestLifetime(time::milliseconds(4000))
+ , m_rrType(RR::NS)
{
}
@@ -31,8 +33,14 @@
}
-void
-Query::fromWire(const Name &name, const Interest &interest)
+bool
+Query::fromInterest(const Name &name, const Interest &interest)
+{
+ return fromInterest(interest);
+}
+
+bool
+Query::fromInterest(const Interest& interest)
{
Name interestName;
interestName = interest.getName();
@@ -51,40 +59,24 @@
if (qtflag == -1)
{
- std::cerr << "There is no QueryType in the Interest Name: " << interestName << std::endl;
- return;
+ std::cerr << "There is no QueryType in the Interest Name: "<< interestName << std::endl;
+ return false;
}
this->m_queryType = toQueryType(interestName.get(qtflag).toUri());
- this->m_rrType = toRRType(interestName.get(len-1).toUri());
+ this->m_rrType = RR::toRRType(interestName.get(len-1).toUri());
this->m_authorityZone = interestName.getPrefix(qtflag); //the DNS/DNS-R is not included
this->m_interestLifetime = interest.getInterestLifetime();
-
-}
-
-template<bool T>
-size_t
-Query::wireEncode(EncodingImpl<T>& block) const
-{
- size_t totalLength = 0;
- totalLength += 0;
-
-
- size_t totalLength = prependByteArrayBlock(block, tlv::Bytes,
- m_key.get().buf(), m_key.get().size());
- totalLength += m_keyName.wireEncode(block);
- totalLength += block.prependVarNumber(totalLength);
- totalLength += block.prependVarNumber(tlv::PublicKey);
-
- return totalLength;
+ this->m_rrLabel = interestName.getSubName(qtflag+1, len-qtflag-2);
+ return true;
}
Interest
-Query::toWire() const
+Query::toInterest() const
{
Name name = this->m_authorityZone;
name.append(toString(this->m_queryType));
name.append(this->m_rrLabel);
- name.append(toString(this->m_rrType));
+ name.append(RR::toString(this->m_rrType));
Selectors selector;
//selector.setMustBeFresh(true);
diff --git a/src/query.hpp b/src/query.hpp
index 48ef464..61b1cff 100644
--- a/src/query.hpp
+++ b/src/query.hpp
@@ -36,6 +36,42 @@
QUERY_DNS_R
};
+ static std::string
+ toString(const QueryType& qType)
+ {
+ std::string label;
+ switch (qType)
+ {
+ case QUERY_DNS:
+ label = "DNS";
+ break;
+ case QUERY_DNS_R:
+ label = "DNS-R";
+ break;
+ default:
+ label = "Default";
+ break;
+ }
+ return label;
+
+ }
+
+ static const QueryType
+ toQueryType(const std::string& str)
+ {
+ QueryType atype;
+ if (str == "DNS") {
+ atype = QUERY_DNS;
+ }
+ else if (str == "DNS-R") {
+ atype = QUERY_DNS_R;
+ }
+ else {
+ atype = QUERY_DNS;
+ }
+ return atype;
+ }
+
Query();
virtual ~Query();
@@ -72,49 +108,14 @@
m_rrLabel = rrLabel;
}
- const RRType& getRrType() const {
+ const RR::RRType& getRrType() const {
return m_rrType;
}
- void setRrType(const RRType& rrType) {
+ void setRrType(const RR::RRType& rrType) {
m_rrType = rrType;
}
- static std::string
- toString(QueryType qType)
- {
- std::string label;
- switch (qType)
- {
- case QUERY_DNS:
- label = "DNS";
- break;
- case QUERY_DNS_R:
- label = "DNS-R";
- break;
- default:
- label = "Default";
- break;
- }
- return label;
-
- }
-
- static const QueryType
- toQueryType(const std::string& str)
- {
- QueryType atype;
- if (str == "DNS") {
- atype = QUERY_DNS;
- }
- else if (str == "DNS-R") {
- atype = QUERY_DNS_R;
- }
- else {
- atype = QUERY_DNS;
- }
- return atype;
- }
private:
template<bool T>
@@ -122,30 +123,38 @@
wireEncode(EncodingImpl<T> & block) const;
public:
-
- const Block&
- wireEncode() const;
-
- void
- wireDecode(const Block& wire);
-
-
Interest
- toWire() const;
+ toInterest() const;
- void
- fromWire(const Name &name, const Interest& interest);
+ bool
+ fromInterest(const Name &name, const Interest& interest);
+
+ bool
+ fromInterest(const Interest& interest);
+
public:
Name m_authorityZone;
enum QueryType m_queryType;
time::milliseconds m_interestLifetime;
Name m_rrLabel;
- enum RRType m_rrType;
+ enum RR::RRType m_rrType;
mutable Block m_wire;
};
+
+inline std::ostream&
+operator<<(std::ostream& os, const Query& query)
+{
+ os<<"Query: authorityZone="<<query.getAuthorityZone().toUri()
+ <<" queryType="<<Query::toString(query.getQueryType())
+ <<" rrLabel="<<query.getRrLabel().toUri()
+ <<" rrType="<<RR::toString(query.getRrType());
+ return os;
+}
+
+
} // namespace ndns
} // namespace ndn
diff --git a/src/resolver.hpp b/src/resolver.hpp
deleted file mode 100644
index d690c43..0000000
--- a/src/resolver.hpp
+++ /dev/null
@@ -1,42 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014, Regents of the University of California.
- *
- * This file is part of NDNS (Named Data Networking Domain Name Service).
- * See AUTHORS.md for complete list of NDNS authors and contributors.
- *
- * NDNS 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.
- *
- * NDNS 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
- * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef NDNS_RESOLVER_HPP
-#define NDNS_RESOLVER_HPP
-
-#include "rr.hpp"
-
-namespace ndn {
-namespace ndns {
-
-class Resolver {
-public:
- Resolver();
- virtual ~Resolver();
-
-
- const RR iterativelyResolve(const std::string& domain, const std::string& name);
- const RR recusivelyResolve(const std::string& domain, const std::string& name);
-
-};
-
-} // namespace ndns
-} // namespace ndn
-
-#endif // NDNS_RESOLVER_HPP
diff --git a/src/response.cpp b/src/response.cpp
index f0c5b17..cb13dda 100644
--- a/src/response.cpp
+++ b/src/response.cpp
@@ -19,11 +19,13 @@
#include "response.hpp"
-
namespace ndn {
namespace ndns {
Response::Response()
+ : m_freshness (time::milliseconds(3600))
+ , m_contentType (Query::QUERY_DNS)
+ , m_responseType (NDNS_Resp)
{
}
@@ -31,96 +33,184 @@
{
}
-
-Data
-Response::toWire() const
+template<bool T>
+size_t
+Response::wireEncode(EncodingImpl<T> & block) const
{
- Name name = this->m_queryName;
- name.append(this->m_serial);
+ size_t totalLength = 0;
+ std::vector<RR>::size_type lenOfRR = m_rrs.size();
+ RR rr;
+ Block btemp;
- Data data(name);
+ //totalLength += m_rrs[0].wireEncode(block);
- data.setFreshnessPeriod(this->m_freshness);
-
- string content = "";
-
- size_t totalLen = 0;
- Block block = Block();
- block.push_back
- (nonNegativeIntegerBlock
- (tlv::ndns::Type, static_cast<unsigned long>(this->m_responseType))
- );
-
- block.push_back
- (nonNegativeIntegerBlock
- (tlv::ndns::Fressness, this->m_freshness.count())
- );
-
- Block block2 = Block(tlv::ndns::ContentBlob);
- block2.push_back
- (nonNegativeIntegerBlock
- (tlv::ndns::NumberOfRRData, this->m_numberOfRR)
- );
-
- for (int i=0; i<this->m_numberOfRR; i++)
+ for (std::vector<RR>::size_type i=0; i<lenOfRR; i++)
{
- RR rr = m_rrs[i];
- block2.push_back(rr.toWire());
+ rr = m_rrs[lenOfRR-i-1];
+ totalLength += rr.wireEncode(block);
+ //std::cout<<"totalLenght="<<totalLength<<std::endl;
}
- block.push_back(block2);
+ totalLength += prependNonNegativeIntegerBlock(block,
+ ndn::ndns::tlv::ResponseNumberOfRRData,
+ lenOfRR);
- return data;
+ totalLength += block.prependVarNumber(totalLength);
+ totalLength += block.prependVarNumber(ndn::ndns::tlv::ResponseContentBlob);
+
+ totalLength += prependNonNegativeIntegerBlock(block,
+ ndn::ndns::tlv::ResponseFressness,
+ m_freshness.count()
+ );
+
+ std::string msg = Response::toString(m_responseType);
+ totalLength += prependByteArrayBlock(block,
+ ndn::ndns::tlv::ResponseType,
+ reinterpret_cast<const uint8_t*>(msg.c_str()),
+ msg.size()
+ );
+
+ totalLength += m_queryName.wireEncode(block);
+
+
+ totalLength += block.prependVarNumber(totalLength);
+ totalLength += block.prependVarNumber(Tlv::Content);
+ return totalLength;
+}
+
+
+const Block&
+Response::wireEncode() const
+{
+
+ if (m_wire.hasWire())
+ return m_wire;
+ EncodingEstimator estimator;
+
+ size_t estimatedSize = wireEncode(estimator);
+ //std::cout<< typeid( this).name()<<" Instance estimatedsize="<<estimatedSize<<std::endl;
+ EncodingBuffer buffer(estimatedSize, 0);
+ wireEncode(buffer);
+ m_wire = buffer.block();
+ return m_wire;
+}
+
+
+void
+Response::wireDecode(const Block& wire)
+{
+ if (!wire.hasWire()) {
+ throw Tlv::Error("The supplied block does not contain wire format");
+ }
+
+ //if (wire.type() != ndn::ndns::tlv::ResponseContentBlob)
+ // throw Tlv::Error("Unexpected TLV type when decoding Content");
+
+ m_wire = wire;
+ m_wire.parse();
+
+
+ Block::element_const_iterator it = m_wire.elements_begin();
+
+ if (it != m_wire.elements_end() && it->type() == ndn::Tlv::Name)
+ {
+ m_queryName.wireDecode(*it);
+ it ++;
+ } else
+ {
+ throw Tlv::Error("not the ndn::Tlv::Name type");
+ }
+
+ if (it != m_wire.elements_end() && it->type() == ndn::ndns::tlv::ResponseType)
+ {
+ std::string temp = std::string(reinterpret_cast<const char*>(it->value()),
+ it->value_size());
+ m_responseType = Response::toResponseType(temp);
+ it ++;
+ } else
+ {
+ throw Tlv::Error("not the ndn::ndns::tlv::ReponseType type");
+ }
+
+ if (it != m_wire.elements_end() && it->type() == ndn::ndns::tlv::ResponseFressness)
+ {
+ m_freshness = time::milliseconds(readNonNegativeInteger(*it));
+ it ++;
+ } else
+ {
+ throw Tlv::Error("not the ndn::ndns::tlv::ReponseFreshness type");
+ }
+
+
+ if (it != m_wire.elements_end() && it->type() == ndn::ndns::tlv::ResponseContentBlob)
+ {
+ //Block b2 = it->value();/* to check */
+ Block b2 = *it;/* to check */
+
+ b2.parse();
+ Block::element_const_iterator it2 = b2.elements_begin();
+ size_t rrlen = 0;
+ if (it2 != b2.elements_end() && it2->type() == ndn::ndns::tlv::ResponseNumberOfRRData)
+ {
+ rrlen = readNonNegativeInteger(*it2);
+ it2 ++;
+ } else
+ {
+ throw Tlv::Error("not the ndn::ndns::tlv::ResponseNumberOfRRData type");
+ }
+ for (size_t i=0; i<rrlen; i++)
+ {
+ if (it2 != b2.elements_end() &&
+ it2->type() == ndn::ndns::tlv::RRData)
+ {
+ RR rr;
+ rr.wireDecode(*it2);
+ this->m_rrs.push_back(rr);
+ it2 ++;
+ } else
+ {
+ throw Tlv::Error("not the ndn::ndns::tlv::RRData type");
+ }
+ }
+
+
+ it ++;
+ } else
+ {
+ throw Tlv::Error("not the ndn::ndns::tlv::ResponseContentBlob type");
+ }
+
+
}
void
-Response::fromWire(const Interest &interest, const Data &data)
+Response::fromData(const Data& data)
{
- Name dataName;
- dataName = data.getName();
- int qtflag = -1;
- size_t len = dataName.size();
- for (size_t i=0; i<len; i++)
- {
- string comp = dataName.get(i).toEscapedString();
- if (comp == ndn::toString(QueryType::DNS) || comp == ndn::toString(QueryType::DNS_R))
- {
- qtflag = i;
- break;
- }
- }//for
+ m_queryName = data.getName();
+ m_freshness = data.getFreshnessPeriod();
+ m_contentType = Query::QueryType(data.getContentType());
+ this->wireDecode(data.getContent());
+}
- if (qtflag == -1)
- {
- cerr<<"There is no QueryType in the Interest Name: "<<dataName<<endl;
- return;
- }
- this->m_queryName = dataName.getPrefix(-1);
+void
+Response::fromData(const Name& name, const Data& data)
+{
+ fromData(data);
+}
- string last = dataName.get(len-1).toEscapedString();
- if (ndn::toRRType(last) == RRType::UNKNOWN)
- {
- this->m_serial = "";
- } else
- {
- this->m_serial = last;
- }
+Data
+Response::toData() const
+{
+ Data data;
+ data.setName(m_queryName);
+ data.setFreshnessPeriod(this->m_freshness);
+ data.setContentType(m_contentType);
+ data.setContent(this->wireEncode());
- Block block = data.getContent();
- this->m_numberOfRR = readNonNegativeInteger(block.get(tlv::ndns::NumberOfRRData));
-
- Block block2 = block.get(tlv::ndns::ContentBlob);
- for (int i=0; i<this->m_numberOfRR; i++)
- {
- Block block3 = block2.get(tlv::ndns::RRData);
- RR rr;
- rr.fromWire(block3);
- m_rrs.push_back(rr);
- }
-
+ return data;
}
diff --git a/src/response.hpp b/src/response.hpp
index 95a4cda..e2266ac 100644
--- a/src/response.hpp
+++ b/src/response.hpp
@@ -36,63 +36,186 @@
#include <vector>
#include "ndns-tlv.hpp"
+#include "query.hpp"
+#include "rr.hpp"
namespace ndn {
namespace ndns {
-enum ResponseType
- {
- NDNS_Resp,
- NDNS_Nack,
- NDNS_Auth
- };
-
-
-static std::string
-toString(ResponseType responseType) const
-{
- string label;
- switch (responseType)
- {
- case ResponseType::NDNS_Resp:
- label = "NDNS Resp";
- break;
- case ResponseType::NDNS_Nack:
- label = "NDNS Nack";
- break;
- case ResponseType::NDNS_Auth:
- label = "NDNS Auth";
- break;
- default:
- label = "Default";
- break;
- }
- return label;
-}
class Response {
public:
+ enum ResponseType
+ {
+ NDNS_Resp,
+ NDNS_Nack,
+ NDNS_Auth,
+ UNKNOWN
+ };
+
+
+ static std::string
+ toString(ResponseType responseType)
+ {
+ std::string label;
+ switch (responseType)
+ {
+ case NDNS_Resp:
+ label = "NDNS Resp";
+ break;
+ case NDNS_Nack:
+ label = "NDNS Nack";
+ break;
+ case NDNS_Auth:
+ label = "NDNS Auth";
+ break;
+ default:
+ label = "UNKNOWN";
+ break;
+ }
+ return label;
+ }
+
+ static ResponseType
+ toResponseType(const std::string& str)
+ {
+ ResponseType atype;
+ if (str == "NDNS Resp"){
+ atype = NDNS_Resp;
+ }
+ else if (str == "NDNS Nack") {
+ atype = NDNS_Nack;
+ }
+ else if (str == "NDNS Auth")
+ {
+ atype = NDNS_Auth;
+ }
+ else {
+ atype = UNKNOWN;
+ }
+ return atype;
+ }
+
Response();
virtual ~Response();
- Data
- toWire() const;
+ inline void addRr(const uint32_t rrId, const std::string rrData)
+ {
+ RR rr;
+ rr.setId(rrId);
+ rr.setRrdata(rrData);
+ this->m_rrs.push_back(rr);
+ }
+
+ const Block&
+ wireEncode() const;
void
- fromWire(const Interest &interest, const Data &data);
+ wireDecode(const Block& wire);
+
+ template<bool T>
+ size_t
+ wireEncode(EncodingImpl<T> & block) const;
+
+
+ void
+ fromData(const Name& name, const Data& data);
+
+ void
+ fromData(const Data &data);
+
+ Data
+ toData() const;
+
+
+
+ const std::string
+ getStringRRs() const {
+ std::stringstream str;
+ str<<"[";
+ std::vector<RR>::const_iterator iter = m_rrs.begin();
+ while (iter != m_rrs.end())
+ {
+ str<<" "<<*iter;
+ iter ++;
+ }
+ str<<"]";
+ return str.str();
+ }
+
+ Query::QueryType getContentType() const {
+ return m_contentType;
+ }
+
+ void setContentType(Query::QueryType contentType) {
+ m_contentType = contentType;
+ }
+
+ time::milliseconds getFreshness() const {
+ return m_freshness;
+ }
+
+ void setFreshness(time::milliseconds freshness) {
+ m_freshness = freshness;
+ }
+
+ const Name& getQueryName() const {
+ return m_queryName;
+ }
+
+ void setQueryName(const Name& queryName) {
+ m_queryName = queryName;
+ }
+
+ ResponseType getResponseType() const {
+ return m_responseType;
+ }
+
+ void setResponseType(ResponseType responseType) {
+ m_responseType = responseType;
+ }
+
+ const std::vector<RR>& getRrs() const {
+ return m_rrs;
+ }
+
+ void setRrs(const std::vector<RR>& rrs) {
+ m_rrs = rrs;
+ }
+
+
private:
- Name m_queryName;
- std::string m_serial;
- ResponseType m_responseType;
-
time::milliseconds m_freshness;
-
- unsigned int m_numberOfRR;
- vector<RR> m_rrs;
+ Name m_queryName;
+ //std::string m_serial;
+ Query::QueryType m_contentType;
+ ResponseType m_responseType;
+ //unsigned int m_numberOfRR;
+ std::vector<RR> m_rrs;
+ mutable Block m_wire;
};
+
+inline std::ostream&
+operator<<(std::ostream& os, const Response& response)
+{
+ os<<"Response: queryName="<<response.getQueryName().toUri()
+ <<" responseType="<<Response::toString(response.getResponseType())
+ <<" contentType="<<Query::toString(response.getContentType())
+ <<" [";
+ std::vector<RR>::const_iterator iter = response.getRrs().begin();
+ while (iter != response.getRrs().end())
+ {
+ os<<" "<<*iter;
+ iter ++;
+ }
+ os<<"]";
+ return os;
+}
+
+
} // namespace ndns
} // namespace ndn
diff --git a/src/rr.cpp b/src/rr.cpp
index 4f55e21..c2700ba 100644
--- a/src/rr.cpp
+++ b/src/rr.cpp
@@ -32,20 +32,9 @@
{
}
-template<bool T>
-size_t
-RR::wireEncode(EncodingImpl<T> & block) const
-{
- size_t totalLength = 0;
- const std::string& msg = this->getRrdata();
- totalLength += prependByteArrayBlock(block,
- tlv::ndns::RRData,
- reinterpret_cast<const uint8_t*>(msg.c_str()),
- msg.size()
- );
- return totalLength;
-}
+
+
const Block&
RR::wireEncode() const
@@ -53,7 +42,9 @@
if (m_wire.hasWire())
return m_wire;
EncodingEstimator estimator;
+
size_t estimatedSize = wireEncode(estimator);
+ //std::cout<<"estmatedSize="<<estimatedSize<<std::endl;
EncodingBuffer buffer(estimatedSize, 0);
wireEncode(buffer);
m_wire = buffer.block();
@@ -67,7 +58,7 @@
throw Tlv::Error("The supplied block does not contain wire format");
}
- if (wire.type() != tlv::ndns::RRData)
+ if (wire.type() != ndn::ndns::tlv::RRData)
throw Tlv::Error("Unexpected TLV type when decoding Content");
m_wire = wire;
@@ -75,14 +66,26 @@
Block::element_const_iterator it = m_wire.elements_begin();
- if (it != m_wire.elements_end() && it->type() == tlv::ndns::RRData)
+ if (it != m_wire.elements_end() && it->type() == ndn::ndns::tlv::RRDataSub1)
{
- m_rrData = std::string(reinterpret_cast<const char*>(it->value()), it->value_size());
+ m_id = readNonNegativeInteger(*it);
it ++;
} else {
- throw Tlv::Error("not the RRData Type");
+ throw Tlv::Error("not the RRDataSub1 Type");
}
+ if (it != m_wire.elements_end() && it->type() == ndn::ndns::tlv::RRDataSub2)
+ {
+
+ m_rrData = std::string(reinterpret_cast<const char*>(it->value()),
+ it->value_size());
+ it ++;
+ } else {
+ throw Tlv::Error("not the RRDataSub2 Type");
+ }
+
+
+
}
} // namespace ndns
diff --git a/src/rr.hpp b/src/rr.hpp
index 95001e9..106bb39 100644
--- a/src/rr.hpp
+++ b/src/rr.hpp
@@ -25,24 +25,64 @@
#include <ndn-cxx/encoding/block.hpp>
#include <ndn-cxx/interest.hpp>
+#include <ndn-cxx/encoding/encoding-buffer.hpp>
+
namespace ndn {
namespace ndns {
-enum RRType
- {
- NS,
- TXT,
- UNKNOWN
- };
class RR {
public:
+
+ enum RRType
+ {
+ NS,
+ TXT,
+ UNKNOWN
+ };
+ static std::string
+ toString(const RRType& type)
+ {
+ std::string str;
+
+ switch (type)
+ {
+ case NS:
+ str = "NS";
+ break;
+ case TXT:
+ str = "TXT";
+ break;
+ default:
+ str = "UNKNOWN";
+ break;
+ }
+ return str;
+ }
+
+ static RRType
+ toRRType(const std::string& str)
+ {
+ RRType atype;
+ if (str == "NS"){
+ atype = NS;
+ }
+ else if (str == "TXT") {
+ atype = TXT;
+ }
+ else {
+ atype = UNKNOWN;
+ }
+ return atype;
+ }
+
RR();
virtual ~RR();
const std::string&
getRrdata() const
{
+
return m_rrData;
}
@@ -51,32 +91,86 @@
this->m_rrData = rrdata;
}
-private:
- template<bool T>
- size_t
- wireEncode(EncodingImpl<T> & block) const;
+
public:
+ uint32_t getId() const {
+ return m_id;
+ }
+
+ void setId(uint32_t id) {
+ m_id = id;
+ }
+
+ const Block& getWire() const {
+ return m_wire;
+ }
+
+ void setWire(const Block& wire) {
+ m_wire = wire;
+ }
+
+
+ inline bool operator==(const RR& rr) const
+ {
+ if (this->getRrdata() == rr.getRrdata())
+ return true;
+
+ return false;
+ }
+
+
+ template<bool T>
+ inline size_t
+ wireEncode(EncodingImpl<T> & block) const
+ {
+ size_t totalLength = 0;
+ const std::string& msg = this->getRrdata();
+ totalLength += prependByteArrayBlock(block,
+ ndn::ndns::tlv::RRDataSub2,
+ reinterpret_cast<const uint8_t*>(msg.c_str()),
+ msg.size()
+ );
+
+ totalLength += prependNonNegativeIntegerBlock(block,
+ ndn::ndns::tlv::RRDataSub1,
+ this->getId());
+
+ totalLength += block.prependVarNumber(totalLength);
+ totalLength += block.prependVarNumber(ndn::ndns::tlv::RRData);
+ //std::cout<<"call rr.h wireEncode"<<std::endl;
+ return totalLength;
+ }
+
+
const Block&
wireEncode() const;
void
wireDecode(const Block& wire);
-
- Interest
- toWire() const;
-
+ //inline std::ostream& operator<<(std::ostream& os, const RR& rr);
private:
uint32_t m_id;
+ //unsigned long m_id;
std::string m_rrData;
mutable Block m_wire;
-};
+};//class RR
+
+
+inline std::ostream&
+operator<<(std::ostream& os, const RR& rr)
+{
+ os<<"RR: Id="<<rr.getId()<<" Data="<<rr.getRrdata();
+ return os;
+}
} // namespace ndns
} // namespace ndn
+
+
#endif // NDNS_RR_HPP
diff --git a/src/zone.cpp b/src/zone.cpp
index f5a33ba..c161cd3 100644
--- a/src/zone.cpp
+++ b/src/zone.cpp
@@ -22,14 +22,19 @@
namespace ndn {
namespace ndns {
-Zone::Zone() {
+Zone::Zone(const Name& name)
+: m_id(0)
+, m_authorizedName(name)
+{
}
-Zone::~Zone() {
+Zone::Zone()
+ : m_id(0)
+{
}
-const RR Zone::hasName(const std::string& key) {
- return "example.key";
+Zone::~Zone()
+{
}
} // namespace ndns
diff --git a/src/zone.hpp b/src/zone.hpp
index a54c9a8..6b62580 100644
--- a/src/zone.hpp
+++ b/src/zone.hpp
@@ -21,18 +21,40 @@
#define NDNS_ZONE_HPP
#include "rr.hpp"
+#include "query.hpp"
+#include "response.hpp"
namespace ndn {
namespace ndns {
class Zone {
+
public:
+
+ Zone(const Name& name);
Zone();
virtual ~Zone();
- const RR hasName(const std::string& key);
+ const Name& getAuthorizedName() const {
+ return m_authorizedName;
+ }
-};
+ void setAuthorizedName(const Name& authorizedName) {
+ m_authorizedName = authorizedName;
+ }
+
+ uint32_t getId() const {
+ return m_id;
+ }
+
+ void setId(uint32_t id) {
+ m_id = id;
+ }
+
+private:
+ uint32_t m_id;
+ Name m_authorizedName;
+};//class Zone
} // namespace ndns
} // namespace ndn