blob: 990ce84fd8ec41229ec390961ca6cf9828e07af4 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2017-2022, Regents of the University of California.
*
* This file is part of ndncert, a certificate management system based on NDN.
*
* ndncert 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.
*
* ndncert 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 copies of the GNU General Public License along with
* ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*
* See AUTHORS.md for complete list of ndncert authors and contributors.
*/
#include "detail/error-encoder.hpp"
NDN_LOG_INIT(ndncert.encode.error);
namespace ndncert {
Block
errortlv::encodeDataContent(ErrorCode errorCode, const std::string& description)
{
Block response(ndn::tlv::Content);
response.push_back(ndn::makeNonNegativeIntegerBlock(tlv::ErrorCode, static_cast<size_t>(errorCode)));
response.push_back(ndn::makeStringBlock(tlv::ErrorInfo, description));
response.encode();
return response;
}
std::tuple<ErrorCode, std::string>
errortlv::decodefromDataContent(const Block& block)
{
try {
block.parse();
int codeCount = 0;
int infoCount = 0;
int otherCriticalCount = 0;
ErrorCode error;
std::string errorInfo;
for (const auto& item : block.elements()) {
if (item.type() == tlv::ErrorCode) {
error = static_cast<ErrorCode>(readNonNegativeInteger(block.get(tlv::ErrorCode)));
codeCount ++;
}
else if (item.type() == tlv::ErrorInfo) {
errorInfo = readString(block.get(tlv::ErrorInfo));
infoCount ++;
}
else if (ndn::tlv::isCriticalType(item.type())) {
otherCriticalCount ++;
}
else {
//ignore
}
}
if (codeCount == 0 && infoCount == 0) {
return std::make_tuple(ErrorCode::NO_ERROR, "");
}
if (codeCount != 1 || infoCount != 1) {
NDN_THROW(std::runtime_error("Error TLV contains " + std::to_string(codeCount) + " error code(s) and " +
std::to_string(infoCount) + "error info(s), instead of expected 1 times each."));
}
if (otherCriticalCount > 0) {
NDN_THROW(std::runtime_error("Unknown Critical TLV type in error packet"));
}
return std::make_tuple(error, errorInfo);
} catch (const std::exception& e) {
NDN_LOG_ERROR("[errortlv::DecodeFromDataContent] Exception in error message decoding: " << e.what());
return std::make_tuple(ErrorCode::NO_ERROR, "");
}
}
} // namespace ndncert