face: Fixing face registration protocol by ensuring uniqueness of each command interest
This commit also introduces a new helper ndn::random::generateWord32()
to generate random 32-bit integer (currently using CryptoPP functions).
Change-Id: I7bd32875cbdd98eea793aa5d8270f8091b7c1a6b
diff --git a/src/interest.cpp b/src/interest.cpp
index 7f2ddc1..6c030dc 100644
--- a/src/interest.cpp
+++ b/src/interest.cpp
@@ -5,23 +5,9 @@
* See COPYING for copyright and distribution information.
*/
-#include <stdexcept>
#include "common.hpp"
#include "interest.hpp"
-
-#if __clang__
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wreorder"
-#pragma clang diagnostic ignored "-Wtautological-compare"
-#pragma clang diagnostic ignored "-Wunused-variable"
-#pragma clang diagnostic ignored "-Wunused-function"
-#elif __GNUC__
-#pragma GCC diagnostic ignored "-Wreorder"
-#pragma GCC diagnostic ignored "-Wunused-variable"
-#pragma GCC diagnostic ignored "-Wunused-function"
-#endif
-
-#include <cryptopp/osrng.h>
+#include "util/random.hpp"
using namespace std;
@@ -32,10 +18,8 @@
const uint32_t&
Interest::getNonce() const
{
- static CryptoPP::AutoSeededRandomPool rng;
-
if (nonce_ == 0)
- nonce_ = rng.GenerateWord32();
+ nonce_ = random::generateWord32();
return nonce_;
}
diff --git a/src/node.cpp b/src/node.cpp
index fb5e086..23e3185 100644
--- a/src/node.cpp
+++ b/src/node.cpp
@@ -5,17 +5,16 @@
* See COPYING for copyright and distribution information.
*/
-#include <stdexcept>
-#include "util/time.hpp"
+#include "node.hpp"
#include "forwarding-entry.hpp"
#include "face-instance.hpp"
-#include "node.hpp"
-
-#include "util/ndnd-id-fetcher.hpp"
-
-#include "security/signature-sha256-with-rsa.hpp"
#include "status-response.hpp"
+#include "security/signature-sha256-with-rsa.hpp"
+
+#include "util/time.hpp"
+#include "util/random.hpp"
+#include "util/ndnd-id-fetcher.hpp"
using namespace std;
#if NDN_CPP_HAVE_CXX11
@@ -131,6 +130,8 @@
{
ForwardingEntry forwardingEntry("unreg", (*i)->getPrefix(), faceId_);
Data data;
+ // This ensures uniqueness of each prefix registration commands
+ data.setName(Name().appendVersion(random::generateWord32()));
data.setContent(forwardingEntry.wireEncode());
SignatureSha256WithRsa signature;
@@ -169,8 +170,11 @@
ForwardingEntry forwardingEntry("selfreg", prefixToRegister->getPrefix(), -1, flags, -1);
Block content = forwardingEntry.wireEncode();
+
// Set the ForwardingEntry as the content of a Data packet and sign.
Data data;
+ // This ensures uniqueness of each prefix registration commands
+ data.setName(Name().appendVersion(random::generateWord32()));
data.setContent(content);
// Create an empty signature, since nobody going to verify it for now
diff --git a/src/util/random.cpp b/src/util/random.cpp
new file mode 100644
index 0000000..5ef2ffc
--- /dev/null
+++ b/src/util/random.cpp
@@ -0,0 +1,35 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "random.hpp"
+
+#if __clang__
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wreorder"
+#pragma clang diagnostic ignored "-Wtautological-compare"
+#pragma clang diagnostic ignored "-Wunused-variable"
+#pragma clang diagnostic ignored "-Wunused-function"
+#elif __GNUC__
+#pragma GCC diagnostic ignored "-Wreorder"
+#pragma GCC diagnostic ignored "-Wunused-variable"
+#pragma GCC diagnostic ignored "-Wunused-function"
+#endif
+
+#include <cryptopp/osrng.h>
+
+namespace ndn {
+namespace random {
+
+uint32_t
+generateWord32()
+{
+ static CryptoPP::AutoSeededRandomPool rng;
+
+ return rng.GenerateWord32();
+}
+
+} // namespace random
+} // namespace ndn
diff --git a/src/util/random.hpp b/src/util/random.hpp
new file mode 100644
index 0000000..bb5a3a6
--- /dev/null
+++ b/src/util/random.hpp
@@ -0,0 +1,20 @@
+/**
+ * Copyright (C) 2013 Regents of the University of California.
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NDN_UTIL_RANDOM_HPP
+#define NDN_UTIL_RANDOM_HPP
+
+#include "../common.hpp"
+
+namespace ndn {
+namespace random {
+
+uint32_t
+generateWord32();
+
+} // namespace random
+} // namespace ndn
+
+#endif // NDN_UTIL_RANDOM_HPP