blob: 990ce84fd8ec41229ec390961ca6cf9828e07af4 [file] [log] [blame]
Zhiyi Zhang46049832020-09-28 17:08:12 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0dc02012021-11-23 22:55:03 -05002/*
Tianyuan Yu13aac732022-03-03 20:59:54 -08003 * Copyright (c) 2017-2022, Regents of the University of California.
Zhiyi Zhang46049832020-09-28 17:08:12 -07004 *
5 * This file is part of ndncert, a certificate management system based on NDN.
6 *
7 * ndncert is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * ndncert 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 General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License along with
16 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ndncert authors and contributors.
19 */
20
Zhiyi Zhangb041d442020-10-22 21:57:11 -070021#include "detail/error-encoder.hpp"
Zhiyi Zhang46049832020-09-28 17:08:12 -070022
Tianyuan Yu13aac732022-03-03 20:59:54 -080023NDN_LOG_INIT(ndncert.encode.error);
24
Zhiyi Zhang46049832020-09-28 17:08:12 -070025namespace ndncert {
26
27Block
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080028errortlv::encodeDataContent(ErrorCode errorCode, const std::string& description)
Zhiyi Zhang46049832020-09-28 17:08:12 -070029{
tylerliu1f480be2020-11-10 13:02:53 -080030 Block response(ndn::tlv::Content);
Davide Pesavento0dc02012021-11-23 22:55:03 -050031 response.push_back(ndn::makeNonNegativeIntegerBlock(tlv::ErrorCode, static_cast<size_t>(errorCode)));
32 response.push_back(ndn::makeStringBlock(tlv::ErrorInfo, description));
Zhiyi Zhang46049832020-09-28 17:08:12 -070033 response.encode();
34 return response;
35}
36
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070037std::tuple<ErrorCode, std::string>
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080038errortlv::decodefromDataContent(const Block& block)
Zhiyi Zhang46049832020-09-28 17:08:12 -070039{
Tianyuan Yu13aac732022-03-03 20:59:54 -080040 try {
41 block.parse();
42 int codeCount = 0;
43 int infoCount = 0;
44 int otherCriticalCount = 0;
45 ErrorCode error;
46 std::string errorInfo;
47 for (const auto& item : block.elements()) {
48 if (item.type() == tlv::ErrorCode) {
49 error = static_cast<ErrorCode>(readNonNegativeInteger(block.get(tlv::ErrorCode)));
50 codeCount ++;
51 }
52 else if (item.type() == tlv::ErrorInfo) {
53 errorInfo = readString(block.get(tlv::ErrorInfo));
54 infoCount ++;
55 }
56 else if (ndn::tlv::isCriticalType(item.type())) {
57 otherCriticalCount ++;
58 }
59 else {
60 //ignore
61 }
62 }
63 if (codeCount == 0 && infoCount == 0) {
64 return std::make_tuple(ErrorCode::NO_ERROR, "");
65 }
66 if (codeCount != 1 || infoCount != 1) {
67 NDN_THROW(std::runtime_error("Error TLV contains " + std::to_string(codeCount) + " error code(s) and " +
68 std::to_string(infoCount) + "error info(s), instead of expected 1 times each."));
69 }
70 if (otherCriticalCount > 0) {
71 NDN_THROW(std::runtime_error("Unknown Critical TLV type in error packet"));
72 }
73 return std::make_tuple(error, errorInfo);
74 } catch (const std::exception& e) {
75 NDN_LOG_ERROR("[errortlv::DecodeFromDataContent] Exception in error message decoding: " << e.what());
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070076 return std::make_tuple(ErrorCode::NO_ERROR, "");
77 }
tylerliu36d97f52020-09-30 22:32:54 -070078}
79
Zhiyi Zhange4891b72020-10-10 15:11:57 -070080} // namespace ndncert