blob: 97a1be8c028782d5f66ae61ecad240842c3f5a9c [file] [log] [blame]
Junxiao Shi0eba1aa2014-10-25 09:54:08 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
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.
20 */
21
22#include "nfd-command-options.hpp"
23
Junxiao Shic6acc7a2015-06-23 10:03:56 -070024#ifdef NDN_MANAGEMENT_NFD_COMMAND_OPTIONS_KEEP_DEPRECATED_SIGNING_PARAMS
25#include "../security/identity-certificate.hpp"
26#include "../security/signing-helpers.hpp"
27#endif // NDN_MANAGEMENT_NFD_COMMAND_OPTIONS_KEEP_DEPRECATED_SIGNING_PARAMS
28
Junxiao Shi0eba1aa2014-10-25 09:54:08 -070029namespace ndn {
30namespace nfd {
31
32const time::milliseconds CommandOptions::DEFAULT_TIMEOUT(10000);
33const Name CommandOptions::DEFAULT_PREFIX("ndn:/localhost/nfd");
34
35CommandOptions::CommandOptions()
36 : m_timeout(DEFAULT_TIMEOUT)
37 , m_prefix(DEFAULT_PREFIX)
Junxiao Shi0eba1aa2014-10-25 09:54:08 -070038{
39}
40
41CommandOptions&
42CommandOptions::setTimeout(const time::milliseconds& timeout)
43{
44 if (timeout <= time::milliseconds::zero()) {
45 throw std::out_of_range("timeout must be positive");
46 }
47
48 m_timeout = timeout;
49 return *this;
50}
51
52CommandOptions&
53CommandOptions::setPrefix(const Name& prefix)
54{
55 m_prefix = prefix;
56 return *this;
57}
58
59CommandOptions&
Junxiao Shic6acc7a2015-06-23 10:03:56 -070060CommandOptions::setSigningInfo(const security::SigningInfo& signingInfo)
61{
62 m_signingInfo = signingInfo;
63 return *this;
64}
65
66#ifdef NDN_MANAGEMENT_NFD_COMMAND_OPTIONS_KEEP_DEPRECATED_SIGNING_PARAMS
67
68CommandOptions::SigningParamsKind
69CommandOptions::getSigningParamsKind() const
70{
71 switch (m_signingInfo.getSignerType()) {
72 case security::SigningInfo::SIGNER_TYPE_NULL:
73 return SIGNING_PARAMS_DEFAULT;
74 case security::SigningInfo::SIGNER_TYPE_ID:
75 return SIGNING_PARAMS_IDENTITY;
76 case security::SigningInfo::SIGNER_TYPE_CERT:
77 return SIGNING_PARAMS_CERTIFICATE;
78 default:
79 throw std::out_of_range("SigningInfo::SignerType is not convertible to CommandOptions::SigningParamsKind");
80 }
81}
82
83const Name&
84CommandOptions::getSigningIdentity() const
85{
86 BOOST_ASSERT(m_signingInfo.getSignerType() == security::SigningInfo::SIGNER_TYPE_ID);
87 return m_signingInfo.getSignerName();
88}
89
90const Name&
91CommandOptions::getSigningCertificate() const
92{
93 BOOST_ASSERT(m_signingInfo.getSignerType() == security::SigningInfo::SIGNER_TYPE_CERT);
94 return m_signingInfo.getSignerName();
95}
96
97CommandOptions&
98CommandOptions::setSigningDefault()
99{
100 m_signingInfo = security::SigningInfo();
101 return *this;
102}
103
104CommandOptions&
Junxiao Shi0eba1aa2014-10-25 09:54:08 -0700105CommandOptions::setSigningIdentity(const Name& identityName)
106{
Junxiao Shic6acc7a2015-06-23 10:03:56 -0700107 m_signingInfo = security::signingByIdentity(identityName);
Junxiao Shi0eba1aa2014-10-25 09:54:08 -0700108 return *this;
109}
110
111CommandOptions&
112CommandOptions::setSigningCertificate(const Name& certificateName)
113{
114 // A valid IdentityCertificate has at least 4 name components,
115 // as it follows `<...>/KEY/<...>/<key-id>/ID-CERT/<version>` naming model.
116 if (certificateName.size() < 4) {
117 throw std::invalid_argument("certificate is invalid");
118 }
119
Junxiao Shic6acc7a2015-06-23 10:03:56 -0700120 m_signingInfo = security::signingByCertificate(certificateName);
Junxiao Shi0eba1aa2014-10-25 09:54:08 -0700121 return *this;
122}
123
124CommandOptions&
125CommandOptions::setSigningCertificate(const IdentityCertificate& certificate)
126{
127 return this->setSigningCertificate(certificate.getName());
128}
129
Junxiao Shic6acc7a2015-06-23 10:03:56 -0700130#endif // NDN_MANAGEMENT_NFD_COMMAND_OPTIONS_KEEP_DEPRECATED_SIGNING_PARAMS
131
Junxiao Shi0eba1aa2014-10-25 09:54:08 -0700132} // namespace nfd
133} // namespace ndn