Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "prefix-announcement.hpp" |
| 23 | #include "encoding/tlv-nfd.hpp" |
| 24 | |
| 25 | #include <boost/lexical_cast.hpp> |
| 26 | |
| 27 | namespace ndn { |
| 28 | |
| 29 | static const name::Component KEYWORD_PA_COMP = "20025041"_block; |
| 30 | |
| 31 | PrefixAnnouncement::PrefixAnnouncement() = default; |
| 32 | |
| 33 | PrefixAnnouncement::PrefixAnnouncement(Data data) |
| 34 | : m_data(std::move(data)) |
| 35 | { |
| 36 | if (m_data->getContentType() != tlv::ContentType_PrefixAnn) { |
| 37 | BOOST_THROW_EXCEPTION(Error("Data is not a prefix announcement: ContentType is " + |
| 38 | boost::lexical_cast<std::string>(m_data->getContentType()))); |
| 39 | } |
| 40 | |
| 41 | const Name& dataName = m_data->getName(); |
| 42 | if (dataName.size() < 3 || dataName[-3] != KEYWORD_PA_COMP || |
| 43 | !dataName[-2].isVersion() || !dataName[-1].isSegment()) { |
| 44 | BOOST_THROW_EXCEPTION(Error("Data is not a prefix announcement: wrong name structure")); |
| 45 | } |
| 46 | m_announcedName = dataName.getPrefix(-3); |
| 47 | |
| 48 | const Block& payload = m_data->getContent(); |
| 49 | payload.parse(); |
| 50 | |
| 51 | m_expiration = time::milliseconds(readNonNegativeInteger(payload.get(tlv::nfd::ExpirationPeriod))); |
| 52 | |
| 53 | auto validityElement = payload.find(tlv::ValidityPeriod); |
| 54 | if (validityElement != payload.elements_end()) { |
| 55 | m_validity.emplace(*validityElement); |
| 56 | } |
| 57 | |
| 58 | for (const Block& element : payload.elements()) { |
| 59 | if (element.type() != tlv::nfd::ExpirationPeriod && element.type() != tlv::ValidityPeriod && |
| 60 | tlv::isCriticalType(element.type())) { |
| 61 | BOOST_THROW_EXCEPTION(Error("unrecognized element of critical type " + |
| 62 | to_string(element.type()))); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | const Data& |
| 68 | PrefixAnnouncement::toData(KeyChain& keyChain, const ndn::security::SigningInfo& si, |
| 69 | optional<uint64_t> version) const |
| 70 | { |
| 71 | if (!m_data) { |
| 72 | Name dataName = m_announcedName; |
| 73 | dataName.append(KEYWORD_PA_COMP); |
| 74 | dataName.appendVersion(version.value_or(time::toUnixTimestamp(time::system_clock::now()).count())); |
| 75 | dataName.appendSegment(0); |
| 76 | m_data.emplace(dataName); |
| 77 | |
| 78 | Block content(tlv::Content); |
| 79 | content.push_back(makeNonNegativeIntegerBlock(tlv::nfd::ExpirationPeriod, |
| 80 | m_expiration.count())); |
| 81 | if (m_validity) { |
| 82 | content.push_back(m_validity->wireEncode()); |
| 83 | } |
| 84 | content.encode(); |
| 85 | m_data->setContent(content); |
| 86 | |
| 87 | keyChain.sign(*m_data, si); |
| 88 | } |
| 89 | return *m_data; |
| 90 | } |
| 91 | |
| 92 | PrefixAnnouncement& |
| 93 | PrefixAnnouncement::setAnnouncedName(Name name) |
| 94 | { |
| 95 | m_data.reset(); |
| 96 | m_announcedName = std::move(name); |
| 97 | return *this; |
| 98 | } |
| 99 | |
| 100 | PrefixAnnouncement& |
| 101 | PrefixAnnouncement::setExpiration(time::milliseconds expiration) |
| 102 | { |
| 103 | if (expiration < 0_ms) { |
| 104 | BOOST_THROW_EXCEPTION(std::invalid_argument("expiration period is negative")); |
| 105 | } |
| 106 | m_data.reset(); |
| 107 | m_expiration = expiration; |
| 108 | return *this; |
| 109 | } |
| 110 | |
| 111 | PrefixAnnouncement& |
| 112 | PrefixAnnouncement::setValidityPeriod(optional<security::ValidityPeriod> validity) |
| 113 | { |
| 114 | m_data.reset(); |
| 115 | m_validity = std::move(validity); |
| 116 | return *this; |
| 117 | } |
| 118 | |
| 119 | } // namespace ndn |