Digest<Hash>&
Digest<Hash>::operator<<(Digest<Hash>& src)
{
  ConstBufferPtr buffer = src.computeDigest();
  update(buffer->get(), buffer->size());

  return *this;
}
예제 #2
0
Component
Component::fromImplicitSha256Digest(const ConstBufferPtr& digest)
{
    if (digest->size() != crypto::SHA256_DIGEST_SIZE)
        BOOST_THROW_EXCEPTION(Error("Cannot create ImplicitSha256DigestComponent (input digest must be " +
                                    to_string(crypto::SHA256_DIGEST_SIZE) + " octets)"));

    return Block(tlv::ImplicitSha256DigestComponent, digest);
}
예제 #3
0
void
MulticastDiscovery::registerHubDiscoveryPrefix(const ConstBufferPtr& buffer)
{
  std::vector<uint64_t> multicastFaces;

  size_t offset = 0;
  while (offset < buffer->size()) {
    bool isOk = false;
    Block block;
    std::tie(isOk, block) = Block::fromBuffer(buffer, offset);
    if (!isOk) {
      std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl;
      break;
    }

    offset += block.size();

    nfd::FaceStatus faceStatus(block);

    ndn::util::FaceUri uri(faceStatus.getRemoteUri());
    if (uri.getScheme() == "udp4") {
      namespace ip = boost::asio::ip;
      boost::system::error_code ec;
      ip::address address = ip::address::from_string(uri.getHost(), ec);

      if (!ec && address.is_multicast()) {
        multicastFaces.push_back(faceStatus.getFaceId());
      }
      else
        continue;
    }
  }

  if (multicastFaces.empty()) {
    m_nextStageOnFailure("No multicast faces available, skipping multicast discovery stage");
  }
  else {
    nfd::ControlParameters parameters;
    parameters
      .setName(LOCALHOP_HUB_DISCOVERY_PREFIX)
      .setCost(1)
      .setExpirationPeriod(time::seconds(30));

    nRequestedRegs = multicastFaces.size();
    nFinishedRegs = 0;

    for (const auto& face : multicastFaces) {
      parameters.setFaceId(face);
      m_controller.start<nfd::RibRegisterCommand>(parameters,
                                                  bind(&MulticastDiscovery::onRegisterSuccess,
                                                       this),
                                                  bind(&MulticastDiscovery::onRegisterFailure,
                                                       this, _1, _2));
    }
  }
}
std::ostream&
operator<<(std::ostream& os, Digest<Hash>& digest)
{
  using namespace CryptoPP;

  std::string output;
  ConstBufferPtr buffer = digest.computeDigest();
  StringSource(buffer->buf(), buffer->size(), true, new HexEncoder(new FileSink(os)));

  return os;
}
예제 #5
0
bool
Validator::verifySignature(const uint8_t* buf, const size_t size, const DigestSha256& sig)
{
  try {
    ConstBufferPtr buffer = crypto::computeSha256Digest(buf, size);
    const Block& sigValue = sig.getValue();

    if (buffer != nullptr &&
        buffer->size() == sigValue.value_size() &&
        buffer->size() == crypto::SHA256_DIGEST_SIZE) {
      const uint8_t* p1 = buffer->buf();
      const uint8_t* p2 = sigValue.value();

      return 0 == memcmp(p1, p2, crypto::SHA256_DIGEST_SIZE);
    }
    else
      return false;
  }
  catch (const CryptoPP::Exception& e) {
    return false;
  }
}
    void
    afterFetchedFaceStatusInformation(const shared_ptr<OBufferStream>& buffer, const Name& remoteName)
    {
        ConstBufferPtr buf = buffer->buf();

        Block block;
        size_t offset = 0;

        std::string currentTime;
        std::tm ctime;
        std::stringstream realEpochTime;
        ndn::time::system_clock::TimePoint realCurrentTime = ndn::time::system_clock::now();
        std::string currentTimeStr = ndn::time::toString(realCurrentTime, "%Y-%m-%dT%H:%M:%S%F");

        strptime(currentTimeStr.c_str(), "%FT%T%Z", &ctime);
        std::string stime(currentTimeStr);
        std::time_t realEpochSeconds = std::mktime(&ctime);
        std::size_t pos = stime.find(".");
        std::string realEpochMilli = stime.substr(pos+1);
        realEpochTime << realEpochSeconds << "." << realEpochMilli;

        CollectorData content;

        currentTime =  realEpochTime.str();

        while (offset < buf->size())
        {
            bool ok = Block::fromBuffer(buf, offset, block);
            if (!ok)
            {
                std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl;
                break;
            }

            offset += block.size();

            nfd::FaceStatus faceStatus(block);

            // take only udp4 and tcp4 faces at the moment
            std::string remoteUri = faceStatus.getRemoteUri();
            if(remoteUri.compare(0,4,"tcp4") != 0 &&
                    remoteUri.compare(0,4,"udp4") != 0)
                continue;

            // take the ip from uri (remove tcp4:// and everything after ':'
            std::size_t strPos = remoteUri.find_last_of(":");
            std::string remoteIp = remoteUri.substr(7,strPos - 7);

            std::unordered_set<std::string>::const_iterator got = m_remoteLinks.find(remoteIp);
            // the link is not requested by the server
            if(got == m_remoteLinks.end())
                continue;

            FaceStatus linkStatus;
            linkStatus.setTx(faceStatus.getNOutBytes());
            linkStatus.setRx(faceStatus.getNInBytes());
            linkStatus.setFaceId(faceStatus.getFaceId());
            linkStatus.setLinkIp(remoteIp);
            linkStatus.setTimestamp(currentTime);

            // remove the remoteIP from the list of links to search and add it to the data packet
            m_remoteLinks.erase(remoteIp);
            content.add(linkStatus);

            if (DEBUG)
                std::cout << "about to send back " << linkStatus.getFaceId() << ": " << linkStatus.getRx() << ", " << linkStatus.getTx() << ", " << linkStatus.getLinkIp() << std::endl;
        }

        if (content.size() != 0)
        {
            ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data>(remoteName);
            data->setContent(content.wireEncode());
            data->setFreshnessPeriod(time::seconds(0));

            m_keyChain.sign(*data);
            m_face.put(*data);
        }
    }