daemon: add NameServer

Change-Id: I64b82a0e9351bcc15691279470c807399480a474
diff --git a/src/clients/response.hpp b/src/clients/response.hpp
index fc49846..240659b 100644
--- a/src/clients/response.hpp
+++ b/src/clients/response.hpp
@@ -38,23 +38,7 @@
 namespace ndns {
 
 /**
- * @brief content when update succeeds
- */
-const Block UPDATE_OK = dataBlock(ndn::tlv::Content, "Update OK", 9);
-
-/**
- * @brief content when update fails because of the out-dated update message
- */
-const Block UPDATE_FAIL_OLD_VERSION = dataBlock(ndn::tlv::Content,
-                                                "Update Fails. Error: old version update", 39);
-
-/**
- * @brief string prefix to judge Update fails or not
- */
-const std::string UPDATE_FAIL("Update Fails. Error: ");
-
-/**
- *@brief Default life time of resource record
+ * @brief Default life time of resource record
  */
 const time::seconds DEFAULT_RR_FRESHNESS_PERIOD(3600);
 
diff --git a/src/daemon/config-file.cpp b/src/daemon/config-file.cpp
new file mode 100644
index 0000000..0522176
--- /dev/null
+++ b/src/daemon/config-file.cpp
@@ -0,0 +1,166 @@
+/* -*- 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/>.
+ */
+
+/**
+ * Copyright (c) 2014  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
+ *
+ * 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 "config-file.hpp"
+#include "logger.hpp"
+
+#include <boost/property_tree/info_parser.hpp>
+#include <fstream>
+
+
+namespace ndn {
+namespace ndns {
+
+NDNS_LOG_INIT("ConfigFile");
+
+void
+ConfigFile::throwErrorOnUnknownSection(const std::string& filename,
+                                       const std::string& sectionName,
+                                       const ConfigSection& section,
+                                       bool isDryRun)
+{
+  std::string msg = "Error processing configuration file ";
+  msg += filename;
+  msg += ": no module subscribed for section \"" + sectionName + "\"";
+
+  throw ConfigFile::Error(msg);
+}
+
+void
+ConfigFile::ignoreUnknownSection(const std::string& filename,
+                                 const std::string& sectionName,
+                                 const ConfigSection& section,
+                                 bool isDryRun)
+{
+  // do nothing
+}
+
+ConfigFile::ConfigFile(UnknownConfigSectionHandler unknownSectionCallback)
+  : m_unknownSectionCallback(unknownSectionCallback)
+{
+}
+
+void
+ConfigFile::addSectionHandler(const std::string& sectionName,
+                              ConfigSectionHandler subscriber)
+{
+  m_subscriptions[sectionName] = subscriber;
+}
+
+void
+ConfigFile::parse(const std::string& filename, bool isDryRun)
+{
+  BOOST_ASSERT(isDryRun == false);
+  std::ifstream inputFile;
+  inputFile.open(filename.c_str());
+  if (!inputFile.good() || !inputFile.is_open())
+    {
+      std::string msg = "Failed to read configuration file: ";
+      msg += filename;
+      throw Error(msg);
+    }
+  parse(inputFile, isDryRun, filename);
+  inputFile.close();
+}
+
+void
+ConfigFile::parse(const std::string& input, bool isDryRun, const std::string& filename)
+{
+  std::istringstream inputStream(input);
+  parse(inputStream, isDryRun, filename);
+}
+
+
+void
+ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
+{
+  try
+    {
+      boost::property_tree::read_info(input, m_global);
+    }
+  catch (const boost::property_tree::info_parser_error& error)
+    {
+      std::stringstream msg;
+      msg << "Failed to parse configuration file";
+      msg << " " << filename;
+      msg << " " << error.message() << " line " << error.line();
+      throw Error(msg.str());
+    }
+
+  process(isDryRun, filename);
+}
+
+void
+ConfigFile::process(bool isDryRun, const std::string& filename)
+{
+  BOOST_ASSERT(isDryRun == false);
+  BOOST_ASSERT(!filename.empty());
+  // NFD_LOG_DEBUG("processing..." << ((isDryRun)?("dry run"):("")));
+
+  if (m_global.begin() == m_global.end())
+    {
+      std::string msg = "Error processing configuration file: ";
+      msg += filename;
+      msg += " no data";
+      throw Error(msg);
+    }
+
+  for (ConfigSection::const_iterator i = m_global.begin(); i != m_global.end(); ++i)
+    {
+      const std::string& sectionName = i->first;
+      const ConfigSection& section = i->second;
+
+      SubscriptionTable::iterator subscriberIt = m_subscriptions.find(sectionName);
+      if (subscriberIt != m_subscriptions.end())
+        {
+          ConfigSectionHandler subscriber = subscriberIt->second;
+          subscriber(section, isDryRun, filename);
+        }
+      else
+        {
+          m_unknownSectionCallback(filename, sectionName, section, isDryRun);
+        }
+    }
+}
+
+} // namespace ndns
+} // namespace ndn
diff --git a/src/daemon/config-file.hpp b/src/daemon/config-file.hpp
new file mode 100644
index 0000000..fca4af2
--- /dev/null
+++ b/src/daemon/config-file.hpp
@@ -0,0 +1,150 @@
+/* -*- 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/>.
+ */
+
+/**
+ * Copyright (c) 2014  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
+ *
+ * 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/>.
+ **/
+
+#ifndef NDNS_DAEMON_CONFIG_FILE_HPP
+#define NDNS_DAEMON_CONFIG_FILE_HPP
+
+#include "common.hpp"
+#include <functional>
+#include <map>
+#include <boost/noncopyable.hpp>
+#include <boost/property_tree/ptree.hpp>
+
+
+namespace ndn {
+namespace ndns {
+
+typedef boost::property_tree::ptree ConfigSection;
+
+/// \brief callback for config file sections
+typedef std::function<void(const ConfigSection& /*section*/,
+                      bool /*isDryRun*/,
+                      const std::string& /*filename*/)> ConfigSectionHandler;
+
+/// \brief callback for config file sections without a subscribed handler
+typedef std::function<void(const std::string& /*filename*/,
+                      const std::string& /*sectionName*/,
+                      const ConfigSection& /*section*/,
+                      bool /*isDryRun*/)> UnknownConfigSectionHandler;
+
+class ConfigFile : boost::noncopyable
+{
+public:
+
+  class Error : public std::runtime_error
+  {
+  public:
+    explicit
+    Error(const std::string& what)
+      : std::runtime_error(what)
+    {
+
+    }
+  };
+
+  ConfigFile(UnknownConfigSectionHandler unknownSectionCallback = throwErrorOnUnknownSection);
+
+  static void
+  throwErrorOnUnknownSection(const std::string& filename,
+                             const std::string& sectionName,
+                             const ConfigSection& section,
+                             bool isDryRun);
+
+  static void
+  ignoreUnknownSection(const std::string& filename,
+                       const std::string& sectionName,
+                       const ConfigSection& section,
+                       bool isDryRun);
+
+  /// \brief setup notification of configuration file sections
+  void
+  addSectionHandler(const std::string& sectionName,
+                    ConfigSectionHandler subscriber);
+
+
+  /**
+   * \param filename file to parse
+   * \param isDryRun true if performing a dry run of configuration, false otherwise
+   * \throws ConfigFile::Error if file not found
+   * \throws ConfigFile::Error if parse error
+   */
+  void
+  parse(const std::string& filename, bool isDryRun);
+
+  /**
+   * \param input configuration (as a string) to parse
+   * \param isDryRun true if performing a dry run of configuration, false otherwise
+   * \param filename optional convenience argument to provide more detailed error messages
+   * \throws ConfigFile::Error if file not found
+   * \throws ConfigFile::Error if parse error
+   */
+  void
+  parse(const std::string& input, bool isDryRun, const std::string& filename);
+
+  /**
+   * \param input stream to parse
+   * \param isDryRun true if performing a dry run of configuration, false otherwise
+   * \param filename optional convenience argument to provide more detailed error messages
+   * \throws ConfigFile::Error if parse error
+   */
+  void
+  parse(std::istream& input, bool isDryRun, const std::string& filename);
+
+private:
+
+  void
+  process(bool isDryRun, const std::string& filename);
+
+private:
+  UnknownConfigSectionHandler m_unknownSectionCallback;
+
+  typedef std::map<std::string, ConfigSectionHandler> SubscriptionTable;
+
+  SubscriptionTable m_subscriptions;
+
+  ConfigSection m_global;
+};
+
+} // namespace ndns
+} // namespace ndn
+
+#endif // NDNS_DAEMON_CONFIG_FILE_HPP
diff --git a/src/daemon/name-server.cpp b/src/daemon/name-server.cpp
new file mode 100644
index 0000000..072e889
--- /dev/null
+++ b/src/daemon/name-server.cpp
@@ -0,0 +1,224 @@
+/* -*- 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"
+#include "logger.hpp"
+#include "clients/response.hpp"
+#include <ndn-cxx/encoding/encoding-buffer.hpp>
+
+namespace ndn {
+namespace ndns {
+NDNS_LOG_INIT("NameServer")
+
+const time::milliseconds NAME_SERVER_DEFAULT_CONTENT_FRESHNESS(4000);
+
+NameServer::NameServer(const Name& zoneName, const Name& certName, Face& face, DbMgr& dbMgr,
+                       KeyChain& keyChain, Validator& validator)
+  : m_zone(zoneName)
+  , m_dbMgr(dbMgr)
+  , m_ndnsPrefix(zoneName)
+  , m_keyPrefix(zoneName)
+  , m_certName(certName)
+  , m_contentFreshness(NAME_SERVER_DEFAULT_CONTENT_FRESHNESS)
+  , m_face(face)
+  , m_keyChain(keyChain)
+  , m_validator(validator)
+{
+  if (!m_keyChain.doesCertificateExist(m_certName)) {
+    NDNS_LOG_FATAL("Certificate: " << m_certName << " does not exist");
+    throw Error("certificate does not exist in the KeyChain: " + m_certName.toUri());
+  }
+
+  m_dbMgr.find(m_zone);
+
+  if (m_zone.getId() == 0) {
+    NDNS_LOG_FATAL("m_zone does not exist: " << zoneName);
+    throw Error("Zone " + zoneName.toUri() + " does not exist in the database");
+  }
+
+  m_ndnsPrefix.append(ndns::label::NDNS_ITERATIVE_QUERY);
+  m_keyPrefix.append(ndns::label::NDNS_CERT_QUERY);
+
+  m_face.setInterestFilter(m_ndnsPrefix,
+                           bind(&NameServer::onInterest, this, _1, _2),
+                           bind(&NameServer::onRegisterFailed, this, _1, _2)
+                           );
+
+  m_face.setInterestFilter(m_keyPrefix,
+                           bind(&NameServer::onInterest, this, _1, _2),
+                           bind(&NameServer::onRegisterFailed, this, _1, _2)
+                           );
+
+  NDNS_LOG_INFO("Zone: " << m_zone.getName() << " binds "
+                << "Prefix: " << m_ndnsPrefix << " and " << m_keyPrefix
+                << " with Certificate: " << m_certName
+                );
+}
+
+void
+NameServer::onInterest(const Name& prefix, const Interest& interest)
+{
+  label::MatchResult re;
+  if (!label::matchName(interest, "", m_zone.getName(), re))
+    return;
+
+  if (re.rrType == ndns::label::NDNS_UPDATE_LABEL) {
+    this->handleUpdate(prefix, interest, re); // NDNS Update
+  }
+  else {
+    this->handleQuery(prefix, interest, re);  // NDNS Iterative query
+  }
+}
+
+void
+NameServer::handleQuery(const Name& prefix, const Interest& interest, const label::MatchResult& re)
+{
+  Rrset rrset(&m_zone);
+  rrset.setLabel(re.rrLabel);
+  rrset.setType(re.rrType);
+
+  NDNS_LOG_TRACE("query record: " << interest.getName());
+
+  if (m_dbMgr.find(rrset)) {
+    // find the record: NDNS-RESP, NDNS-AUTH, NDNS-RAW, or NDNS-NACK
+    shared_ptr<Data> answer = make_shared<Data>(rrset.getData());
+    NDNS_LOG_TRACE("answer query with existing Data: " << answer->getName());
+    m_face.put(*answer);
+  }
+  else {
+    // no record, construct NACK
+    Block block = nonNegativeIntegerBlock(::ndn::ndns::tlv::NdnsType, NDNS_NACK);
+    MetaInfo info;
+    info.addAppMetaInfo(block);
+    info.setFreshnessPeriod(this->getContentFreshness());
+    Name name = interest.getName();
+    name.appendVersion();
+    shared_ptr<Data> answer = make_shared<Data>(name);
+    answer->setMetaInfo(info);
+
+    m_keyChain.sign(*answer, m_certName);
+    NDNS_LOG_TRACE("answer query with NDNS-NACK: " << answer->getName());
+    m_face.put(*answer);
+  }
+}
+
+void
+NameServer::handleUpdate(const Name& prefix, const Interest& interest, const label::MatchResult& re)
+{
+  if (re.rrLabel.size() == 1) {
+    // for current, we only allow Update message contains one Data, and ignore others
+    auto it = re.rrLabel.begin();
+    shared_ptr<Data> data;
+    try {
+      // blockFromValue may throw exception, which should not lead to failure of name server
+      const Block& block = it->blockFromValue();
+      data = make_shared<Data>(block);
+    }
+    catch (std::exception& e) {
+      NDNS_LOG_WARN("exception when getting update info: " << e.what());
+      return;
+    }
+    m_validator.validate(*data,
+                         bind(&NameServer::doUpdate, this, interest.shared_from_this(), data),
+                         [this] (const shared_ptr<const Data>& data, const std::string& msg) {
+                           NDNS_LOG_WARN("Ignoring update that did not pass the verification");
+                         });
+  }
+}
+
+void
+NameServer::onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
+{
+  NDNS_LOG_FATAL("fail to register prefix=" << prefix << ". Due to: " << reason);
+  throw Error("zone " + m_zone.getName().toUri() + " register prefix: " +
+              prefix.toUri() + " fails. due to: " + reason);
+}
+
+void
+NameServer::doUpdate(const shared_ptr<const Interest>& interest,
+                     const shared_ptr<const Data>& data)
+{
+  label::MatchResult re;
+  try {
+    if (!label::matchName(*data, "", m_zone.getName(), re))
+      return;
+  }
+  catch (std::exception& e) {
+    NDNS_LOG_INFO("Error while name/certificate matching: " << e.what());
+  }
+
+  Rrset rrset(&m_zone);
+  rrset.setLabel(re.rrLabel);
+  rrset.setType(re.rrType);
+
+  Block ndnsType = nonNegativeIntegerBlock(::ndn::ndns::tlv::NdnsType, NDNS_RESP);
+  MetaInfo info;
+  info.addAppMetaInfo(ndnsType);
+  info.setFreshnessPeriod(this->getContentFreshness());
+  Name name = interest->getName();
+  name.appendVersion();
+  shared_ptr<Data> answer = make_shared<Data>(name);
+  answer->setMetaInfo(info);
+
+  Block blk(ndn::ndns::tlv::RrData);
+  try {
+    if (m_dbMgr.find(rrset)) {
+      const name::Component& newVersion = re.version;
+      if (newVersion > rrset.getVersion()) {
+        // update existing record
+        rrset.setVersion(newVersion);
+        rrset.setData(data->wireEncode());
+        m_dbMgr.update(rrset);
+        blk.push_back(nonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_OK));
+        blk.encode(); // must
+        answer->setContent(blk);
+        NDNS_LOG_TRACE("replace old record and answer update with UPDATE_OK");
+      }
+      else {
+        blk.push_back(nonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_FAILURE));
+        blk.encode();
+        answer->setContent(blk);
+        NDNS_LOG_TRACE("answer update with UPDATE_FAILURE");
+      }
+    }
+    else {
+      // insert new record
+      rrset.setVersion(re.version);
+      rrset.setData(data->wireEncode());
+      rrset.setTtl(m_zone.getTtl());
+      m_dbMgr.insert(rrset);
+      blk.push_back(nonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_OK));
+      blk.encode();
+      answer->setContent(blk);
+      NDNS_LOG_TRACE("insert new record and answer update with UPDATE_OK");
+    }
+  }
+  catch (std::exception& e) {
+    blk.push_back(nonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_FAILURE));
+    blk.encode(); // must
+    answer->setContent(blk);
+    NDNS_LOG_INFO("Error processing the update: " << e.what());
+    NDNS_LOG_TRACE("exception happens and answer update with UPDATE_FAILURE");
+  }
+  m_keyChain.sign(*answer, m_certName);
+  m_face.put(*answer);
+}
+
+} // namespace ndns
+} // namespace ndn
diff --git a/src/daemon/name-server.hpp b/src/daemon/name-server.hpp
new file mode 100644
index 0000000..09c3101
--- /dev/null
+++ b/src/daemon/name-server.hpp
@@ -0,0 +1,141 @@
+/* -*- 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_DAEMON_NAME_SERVER_HPP
+#define NDNS_DAEMON_NAME_SERVER_HPP
+
+#include "zone.hpp"
+#include "rrset.hpp"
+#include "db-mgr.hpp"
+#include "ndns-label.hpp"
+#include "ndns-tlv.hpp"
+#include "validator.hpp"
+#include "common.hpp"
+
+#include <ndn-cxx/security/key-chain.hpp>
+#include <ndn-cxx/face.hpp>
+
+#include <boost/asio.hpp>
+#include <boost/bind.hpp>
+#include <boost/noncopyable.hpp>
+
+#include <sstream>
+#include <stdexcept>
+
+namespace ndn {
+namespace ndns {
+
+/**
+ * @brief provides authoritative name server.
+ *
+ * The authoritative name server handles NDNS query and update.
+ */
+class NameServer : noncopyable
+{
+  DEFINE_ERROR(Error, std::runtime_error);
+
+public:
+  explicit
+  NameServer(const Name& zoneName, const Name& certName, Face& face, DbMgr& dbMgr,
+             KeyChain& keyChain, Validator& validator);
+
+NDNS_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+  void
+  onInterest(const Name& prefix, const Interest &interest);
+
+  /**
+   * @brief handle NDNS query message
+   */
+  void
+  handleQuery(const Name& prefix, const Interest& interest, const label::MatchResult& re);
+
+  /**
+   * @brief handle NDNS update message
+   */
+  void
+  handleUpdate(const Name& prefix, const Interest& interest, const label::MatchResult& re);
+
+  void
+  onRegisterFailed(const ndn::Name& prefix, const std::string& reason);
+
+  void
+  doUpdate(const shared_ptr<const Interest>& interest, const shared_ptr<const Data>& data);
+
+public:
+  const Name&
+  getNdnsPrefix()
+  {
+    return m_ndnsPrefix;
+  }
+
+  const Name&
+  getKeyPrefix() const
+  {
+    return m_keyPrefix;
+  }
+
+  void
+  setKeyPrefix(const Name& keyPrefix)
+  {
+    m_keyPrefix = keyPrefix;
+  }
+
+  const Zone&
+  getZone() const
+  {
+    return m_zone;
+  }
+
+  const time::milliseconds&
+  getContentFreshness() const
+  {
+    return m_contentFreshness;
+  }
+
+  /**
+   * @brief set the Data fresh period
+   */
+  void
+  setContentFreshness(const time::milliseconds& contentFreshness)
+  {
+    BOOST_ASSERT(contentFreshness > time::milliseconds::zero());
+    m_contentFreshness = contentFreshness;
+  }
+
+private:
+  Zone m_zone;
+  DbMgr& m_dbMgr;
+
+  // Name m_hint;
+
+  Name m_ndnsPrefix;
+  Name m_keyPrefix;
+  Name m_certName;
+
+  time::milliseconds m_contentFreshness;
+
+  Face& m_face;
+  KeyChain& m_keyChain;
+  Validator& m_validator;
+};
+
+} // namespace ndns
+} // namespace ndn
+
+#endif // NDNS_DAEMON_NAME_SERVER_HPP
diff --git a/src/daemon/rrset.cpp b/src/daemon/rrset.cpp
index 4559479..96ffce7 100644
--- a/src/daemon/rrset.cpp
+++ b/src/daemon/rrset.cpp
@@ -32,7 +32,7 @@
 operator<<(std::ostream& os, const Rrset& rrset)
 {
   os << "Rrset: Id=" << rrset.getId();
-  if (rrset.getZone() != 0)
+  if (rrset.getZone() != nullptr)
     os << " Zone=(" << *rrset.getZone() << ")";
 
   os << " Label=" << rrset.getLabel()
@@ -41,5 +41,21 @@
   return os;
 }
 
+bool
+Rrset::operator==(const Rrset& other) const
+{
+  if (getZone() == nullptr && other.getZone() != nullptr)
+    return false;
+  else if (getZone() != nullptr && other.getZone() == nullptr)
+    return false;
+  else if (getZone() == nullptr && other.getZone() == nullptr)
+    return (getLabel() == other.getLabel() &&
+            getType() == other.getType() && getVersion() == other.getVersion());
+  else
+    return (*getZone() == *other.getZone() && getLabel() == other.getLabel() &&
+            getType() == other.getType() && getVersion() == other.getVersion());
+
+}
+
 } // namespace ndns
 } // namespace ndn
diff --git a/src/daemon/rrset.hpp b/src/daemon/rrset.hpp
index 69b311b..aaedcfe 100644
--- a/src/daemon/rrset.hpp
+++ b/src/daemon/rrset.hpp
@@ -17,8 +17,8 @@
  * NDNS, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef NDNS_RRSET_HPP
-#define NDNS_RRSET_HPP
+#ifndef NDNS_DAEMON_RRSET_HPP
+#define NDNS_DAEMON_RRSET_HPP
 
 #include "zone.hpp"
 
@@ -36,7 +36,7 @@
 {
 public:
   explicit
-  Rrset(Zone* zone = 0);
+  Rrset(Zone* zone = nullptr);
 
   /**
    * @brief get the id
@@ -174,11 +174,7 @@
    * Note that comparison ignores id, TTL, and Data when comparing RR sets
    */
   bool
-  operator==(const Rrset& other) const
-  {
-    return (getZone() == other.getZone() && getLabel() == other.getLabel() &&
-            getType() == other.getType() && getVersion() == other.getVersion());
-  }
+  operator==(const Rrset& other) const;
 
   /**
    * @brief compare two rrset instance
@@ -207,4 +203,4 @@
 } // namespace ndns
 } // namespace ndn
 
-#endif // NDNS_RRSET_HPP
+#endif // NDNS_DAEMON_RRSET_HPP
diff --git a/src/daemon/zone.hpp b/src/daemon/zone.hpp
index 4eab6a9..e3a15d4 100644
--- a/src/daemon/zone.hpp
+++ b/src/daemon/zone.hpp
@@ -17,8 +17,8 @@
  * NDNS, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef NDNS_ZONE_HPP
-#define NDNS_ZONE_HPP
+#ifndef NDNS_DAEMON_ZONE_HPP
+#define NDNS_DAEMON_ZONE_HPP
 
 #include <ndn-cxx/name.hpp>
 
@@ -133,4 +133,4 @@
 } // namespace ndns
 } // namespace ndn
 
-#endif // NDNS_ZONE_HPP
+#endif // NDNS_DAEMON_ZONE_HPP
diff --git a/src/ndns-enum.hpp b/src/ndns-enum.hpp
index be48bd1..39e77b0 100644
--- a/src/ndns-enum.hpp
+++ b/src/ndns-enum.hpp
@@ -40,6 +40,14 @@
 std::ostream&
 operator<<(std::ostream& os, const NdnsType ndnsType);
 
+/**
+ * @brief define Return code of Update's Response
+ */
+enum UpdateReturnCode {
+  UPDATE_OK = 0, ///< Update succeeds
+  UPDATE_FAILURE = 1 ///< Update fails
+};
+
 } // namespace ndns
 } // namespace ndn
 
diff --git a/src/ndns-tlv.hpp b/src/ndns-tlv.hpp
index 3575ab0..3f8399d 100644
--- a/src/ndns-tlv.hpp
+++ b/src/ndns-tlv.hpp
@@ -32,7 +32,10 @@
 enum {
   NdnsType = 180, ///< Detailed Types are defined in NdnsType in ndns-enum.hpp
   Rr = 190,
-  RrData = 191
+  RrData = 191,
+
+  UpdateReturnCode = 160,
+  UpdateReturnMsg = 161
 };
 
 } // namespace tlv