Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 9510c91 | 2024-02-25 17:50:05 -0500 | [diff] [blame] | 3 | * Copyright (c) 2017-2024, Regents of the University of California. |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 4 | * |
| 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 Zhang | b041d44 | 2020-10-22 21:57:11 -0700 | [diff] [blame] | 21 | #include "detail/error-encoder.hpp" |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 22 | |
Davide Pesavento | 9510c91 | 2024-02-25 17:50:05 -0500 | [diff] [blame] | 23 | #include <ndn-cxx/util/logger.hpp> |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 25 | namespace ndncert::errortlv { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 26 | |
Davide Pesavento | 9510c91 | 2024-02-25 17:50:05 -0500 | [diff] [blame] | 27 | NDN_LOG_INIT(ndncert.encode.error); |
| 28 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 29 | Block |
Davide Pesavento | 6866b90 | 2024-12-22 23:11:26 -0500 | [diff] [blame] | 30 | encodeDataContent(ErrorCode errorCode, std::string_view description) |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 31 | { |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 32 | Block response(ndn::tlv::Content); |
Davide Pesavento | 6866b90 | 2024-12-22 23:11:26 -0500 | [diff] [blame] | 33 | response.push_back(ndn::makeNonNegativeIntegerBlock(tlv::ErrorCode, static_cast<uint64_t>(errorCode))); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 34 | response.push_back(ndn::makeStringBlock(tlv::ErrorInfo, description)); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 35 | response.encode(); |
| 36 | return response; |
| 37 | } |
| 38 | |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 39 | std::tuple<ErrorCode, std::string> |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 40 | decodefromDataContent(const Block& block) |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 41 | { |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 42 | try { |
| 43 | block.parse(); |
Davide Pesavento | 6a625d8 | 2023-02-24 03:38:06 -0500 | [diff] [blame] | 44 | |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 45 | int codeCount = 0; |
| 46 | int infoCount = 0; |
| 47 | int otherCriticalCount = 0; |
Davide Pesavento | 6a625d8 | 2023-02-24 03:38:06 -0500 | [diff] [blame] | 48 | ErrorCode error = ErrorCode::NO_ERROR; |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 49 | std::string errorInfo; |
| 50 | for (const auto& item : block.elements()) { |
| 51 | if (item.type() == tlv::ErrorCode) { |
Davide Pesavento | 6a625d8 | 2023-02-24 03:38:06 -0500 | [diff] [blame] | 52 | error = ndn::readNonNegativeIntegerAs<ErrorCode>(block.get(tlv::ErrorCode)); |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 53 | codeCount++; |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 54 | } |
| 55 | else if (item.type() == tlv::ErrorInfo) { |
| 56 | errorInfo = readString(block.get(tlv::ErrorInfo)); |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 57 | infoCount++; |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 58 | } |
| 59 | else if (ndn::tlv::isCriticalType(item.type())) { |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 60 | otherCriticalCount++; |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 61 | } |
| 62 | else { |
Davide Pesavento | 6a625d8 | 2023-02-24 03:38:06 -0500 | [diff] [blame] | 63 | // ignore |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 64 | } |
| 65 | } |
Davide Pesavento | 6a625d8 | 2023-02-24 03:38:06 -0500 | [diff] [blame] | 66 | |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 67 | if (codeCount == 0 && infoCount == 0) { |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 68 | return {ErrorCode::NO_ERROR, ""}; |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 69 | } |
| 70 | if (codeCount != 1 || infoCount != 1) { |
Davide Pesavento | 6866b90 | 2024-12-22 23:11:26 -0500 | [diff] [blame] | 71 | NDN_THROW(std::runtime_error("Error TLV contains " + std::to_string(codeCount) + |
| 72 | " error code(s) and " + std::to_string(infoCount) + |
| 73 | " error info(s), instead of expected 1 time each.")); |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 74 | } |
| 75 | if (otherCriticalCount > 0) { |
Davide Pesavento | 6a625d8 | 2023-02-24 03:38:06 -0500 | [diff] [blame] | 76 | NDN_THROW(std::runtime_error("Unknown critical TLV type in error packet")); |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 77 | } |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 78 | return {error, errorInfo}; |
| 79 | } |
| 80 | catch (const std::exception& e) { |
| 81 | NDN_LOG_ERROR("Exception in error message decoding: " << e.what()); |
| 82 | return {ErrorCode::NO_ERROR, ""}; |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 83 | } |
tylerliu | 36d97f5 | 2020-09-30 22:32:54 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 86 | } // namespace ndncert::errortlv |