nlsr: discover Faces from NFD

refs: #2954

Change-Id: I072972d88bce0e1012e96f33577657048b7df1e1
diff --git a/src/validator.cpp b/src/validator.cpp
new file mode 100644
index 0000000..a73c9ee
--- /dev/null
+++ b/src/validator.cpp
@@ -0,0 +1,89 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2017,  The University of Memphis,
+ *                           Regents of the University of California
+ *
+ * This file is part of NLSR (Named-data Link State Routing).
+ * See AUTHORS.md for complete list of NLSR authors and contributors.
+ *
+ * NLSR 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.
+ *
+ * NLSR 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
+ * NLSR, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+#include "validator.hpp"
+
+namespace nlsr {
+
+void
+Validator::checkPolicy(const ndn::Data& data, int nSteps, const ndn::OnDataValidated& onValidated,
+                       const ndn::OnDataValidationFailed& onValidationFailed,
+                       std::vector<shared_ptr<ndn::ValidationRequest>>& nextSteps)
+{
+  if (!m_shouldValidate) {
+    onValidated(data.shared_from_this());
+  }
+  else {
+    ValidatorConfig::checkPolicy(data, nSteps, onValidated, onValidationFailed, nextSteps);
+  }
+}
+
+void
+Validator::afterCheckPolicy(const NextSteps& nextSteps, const OnFailure& onFailure)
+{
+  if (m_face == nullptr) {
+    onFailure("Require more information to validate the packet!");
+    return;
+  }
+
+  for (const std::shared_ptr<ndn::ValidationRequest>& request : nextSteps) {
+
+    ndn::Interest& interest = request->m_interest;
+
+    // Look for certificate in permanent storage
+    std::shared_ptr<const ndn::IdentityCertificate> cert = m_certStore.find(interest.getName());
+
+    if (cert != nullptr) {
+      // If the certificate is found, no reason to express interest
+      std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(interest.getName());
+      data->setContent(cert->wireEncode());
+
+      Validator::onData(interest, *data, request);
+    }
+    else {
+      // Prepend broadcast prefix to interest name
+      ndn::Name broadcastName = m_broadcastPrefix;
+      broadcastName.append(interest.getName());
+      interest.setName(broadcastName);
+
+      // Attempt to fetch the certificate
+      m_face->expressInterest(interest,
+                              std::bind(&Validator::onData, this, _1, _2, request),
+                              std::bind(&Validator::onTimeout, // Nack
+                                        this, _1, request->m_nRetries,
+                                        onFailure,
+                                        request),
+                              std::bind(&Validator::onTimeout,
+                                        this, _1, request->m_nRetries,
+                                        onFailure,
+                                        request));
+    }
+  }
+}
+
+std::shared_ptr<const ndn::Data>
+Validator::preCertificateValidation(const ndn::Data& data)
+{
+  std::shared_ptr<ndn::Data> internalData = std::make_shared<ndn::Data>();
+  internalData->wireDecode(data.getContent().blockFromValue());
+  return internalData;
+}
+
+} // namespace nlsr