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