Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 22 | #ifndef NDN_TOOLS_NDNSEC_UTIL_HPP |
| 23 | #define NDN_TOOLS_NDNSEC_UTIL_HPP |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 24 | |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 25 | #include "encoding/buffer-stream.hpp" |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 26 | #include "security/transform.hpp" |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 27 | #include "security/v1/key-chain.hpp" |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 28 | #include "util/io.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 30 | #include <fstream> |
| 31 | #include <iostream> |
| 32 | #include <string> |
| 33 | |
| 34 | #include <boost/asio.hpp> |
| 35 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 36 | #include <boost/exception/all.hpp> |
| 37 | #include <boost/program_options/options_description.hpp> |
| 38 | #include <boost/program_options/parsers.hpp> |
| 39 | #include <boost/program_options/variables_map.hpp> |
| 40 | #include <boost/tokenizer.hpp> |
| 41 | |
| 42 | namespace ndn { |
| 43 | namespace ndnsec { |
| 44 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 45 | bool |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 46 | getPassword(std::string& password, const std::string& prompt); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 47 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 48 | shared_ptr<security::v1::IdentityCertificate> |
| 49 | getIdentityCertificate(const std::string& fileName); |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * @brief An accumulating option value to handle multiple incrementing options. |
| 53 | * |
| 54 | * Based on https://gitorious.org/bwy/bwy/source/8753148c324ddfacb1f3cdc315650586bd7b75a4:use/accumulator.hpp |
| 55 | * @sa http://benjaminwolsey.de/node/103 |
| 56 | */ |
| 57 | template<typename T> |
| 58 | class AccumulatorType : public boost::program_options::value_semantic |
| 59 | { |
| 60 | public: |
| 61 | explicit |
| 62 | AccumulatorType(T* store) |
| 63 | : m_store(store) |
| 64 | , m_interval(1) |
| 65 | , m_default(0) |
| 66 | { |
| 67 | } |
| 68 | |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 69 | /// @brief Set the default value for this option. |
| 70 | AccumulatorType* |
| 71 | setDefaultValue(const T& t) |
| 72 | { |
| 73 | m_default = t; |
| 74 | return this; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @brief Set the interval for this option. |
| 79 | * |
| 80 | * Unlike for program_options::value, this specifies a value |
| 81 | * to be applied on each occurrence of the option. |
| 82 | */ |
| 83 | AccumulatorType* |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 84 | setInterval(const T& t) |
| 85 | { |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 86 | m_interval = t; |
| 87 | return this; |
| 88 | } |
| 89 | |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 90 | std::string |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 91 | name() const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 92 | { |
| 93 | return std::string(); |
| 94 | } |
| 95 | |
| 96 | // There are no tokens for an AccumulatorType |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 97 | unsigned |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 98 | min_tokens() const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 99 | { |
| 100 | return 0; |
| 101 | } |
| 102 | |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 103 | unsigned |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 104 | max_tokens() const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 105 | { |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | // Accumulating from different sources is silly. |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 110 | bool |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 111 | is_composing() const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 112 | { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | // Requiring one or more appearances is unlikely. |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 117 | bool |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 118 | is_required() const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 119 | { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @brief Parse options |
| 125 | * |
| 126 | * Every appearance of the option simply increments the value |
| 127 | * There should never be any tokens. |
| 128 | */ |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 129 | void |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 130 | parse(boost::any& value_store, const std::vector<std::string>& new_tokens, bool utf8) const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 131 | { |
| 132 | if (value_store.empty()) |
| 133 | value_store = T(); |
| 134 | boost::any_cast<T&>(value_store) += m_interval; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @brief If the option doesn't appear, this is the default value. |
| 139 | */ |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 140 | bool |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 141 | apply_default(boost::any& value_store) const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 142 | { |
| 143 | value_store = m_default; |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @brief Notify the user function with the value of the value store. |
| 149 | */ |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 150 | void |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 151 | notify(const boost::any& value_store) const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 152 | { |
| 153 | const T* val = boost::any_cast<T>(&value_store); |
| 154 | if (m_store) |
| 155 | *m_store = *val; |
| 156 | } |
| 157 | |
Alexander Afanasyev | ae20525 | 2015-08-24 14:08:46 -0700 | [diff] [blame] | 158 | #if BOOST_VERSION >= 105900 |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 159 | bool |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 160 | adjacent_tokens_only() const final |
Alexander Afanasyev | ae20525 | 2015-08-24 14:08:46 -0700 | [diff] [blame] | 161 | { |
| 162 | return false; |
| 163 | } |
| 164 | #endif // BOOST_VERSION >= 105900 |
| 165 | |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 166 | private: |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 167 | T* m_store; |
| 168 | T m_interval; |
| 169 | T m_default; |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 170 | }; |
| 171 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 172 | template <typename T> |
| 173 | AccumulatorType<T>* |
| 174 | accumulator() |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 175 | { |
| 176 | return new AccumulatorType<T>(0); |
| 177 | } |
| 178 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 179 | template <typename T> |
| 180 | AccumulatorType<T>* |
| 181 | accumulator(T* store) |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 182 | { |
| 183 | return new AccumulatorType<T>(store); |
| 184 | } |
| 185 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 186 | } // namespace ndnsec |
| 187 | } // namespace ndn |
| 188 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 189 | #endif // NDN_TOOLS_NDNSEC_UTIL_HPP |