hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 5149320 | 2016-07-23 02:03:08 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | 5149320 | 2016-07-23 02:03:08 +0000 | [diff] [blame] | 24 | */ |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 25 | |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame^] | 26 | #include "core/version.hpp" |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 27 | #include <ndn-cxx/encoding/tlv-nfd.hpp> |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 28 | #include <ndn-cxx/face.hpp> |
| 29 | #include <ndn-cxx/security/key-chain.hpp> |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 30 | |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 31 | namespace ndn { |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 32 | |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 33 | const static Name LOCALHOP_HUB = "/localhop/ndn-autoconf/hub"; |
Minsheng Zhang | 60d06d8 | 2015-02-10 14:09:01 -0700 | [diff] [blame] | 34 | const static Name LOCALHOP_ROUTABLE_PREFIXES = "/localhop/nfd/rib/routable-prefixes"; |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 35 | |
| 36 | static void |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 37 | usage(const char* programName) |
| 38 | { |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 39 | std::cout << "Usage:\n" << programName << " [-h] [-V] [-p prefix] [-p prefix] ... Uri \n" |
| 40 | << " -h - print usage and exit\n" |
| 41 | << " -V - print version number and exit\n" |
| 42 | << " -p prefix - the local prefix of the hub\n" |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 43 | << "\n" |
| 44 | << " Uri - a FaceMgmt URI\n" |
| 45 | << std::endl; |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 48 | class PrefixCollection : noncopyable |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 49 | { |
| 50 | public: |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 51 | bool |
| 52 | empty() const |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 53 | { |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 54 | return m_prefixes.empty(); |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 55 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 56 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 57 | void |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 58 | add(const Name& prefix) |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 59 | { |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 60 | m_prefixes.push_back(prefix); |
| 61 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 62 | |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 63 | template<bool T> |
| 64 | size_t |
| 65 | wireEncode(EncodingImpl<T>& encoder) const |
| 66 | { |
| 67 | size_t totalLength = 0; |
| 68 | |
| 69 | for (std::vector<Name>::const_reverse_iterator i = m_prefixes.rbegin(); |
| 70 | i != m_prefixes.rend(); ++i) { |
| 71 | totalLength += i->wireEncode(encoder); |
| 72 | } |
| 73 | |
| 74 | totalLength += encoder.prependVarNumber(totalLength); |
| 75 | totalLength += encoder.prependVarNumber(tlv::Content); |
| 76 | return totalLength; |
| 77 | } |
| 78 | |
| 79 | Block |
| 80 | wireEncode() const |
| 81 | { |
| 82 | Block block; |
| 83 | |
| 84 | EncodingEstimator estimator; |
| 85 | size_t estimatedSize = wireEncode(estimator); |
| 86 | |
| 87 | EncodingBuffer buffer(estimatedSize); |
| 88 | wireEncode(buffer); |
| 89 | |
| 90 | return buffer.block(); |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | std::vector<Name> m_prefixes; |
| 95 | }; |
| 96 | |
| 97 | class NdnAutoconfigServer : noncopyable |
| 98 | { |
| 99 | public: |
| 100 | NdnAutoconfigServer(const std::string& hubFaceUri, const PrefixCollection& routablePrefixes) |
| 101 | { |
| 102 | KeyChain m_keyChain; |
| 103 | |
| 104 | // pre-create hub Data |
| 105 | m_hubData = make_shared<Data>(Name(LOCALHOP_HUB).appendVersion()); |
| 106 | m_hubData->setFreshnessPeriod(time::hours(1)); // 1 hour |
Junxiao Shi | 5149320 | 2016-07-23 02:03:08 +0000 | [diff] [blame] | 107 | m_hubData->setContent(makeBinaryBlock(tlv::nfd::Uri, |
| 108 | reinterpret_cast<const uint8_t*>(hubFaceUri.c_str()), |
| 109 | hubFaceUri.size())); |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 110 | m_keyChain.sign(*m_hubData); |
| 111 | |
| 112 | // pre-create routable prefix Data |
| 113 | if (!routablePrefixes.empty()) { |
Minsheng Zhang | 60d06d8 | 2015-02-10 14:09:01 -0700 | [diff] [blame] | 114 | Name routablePrefixesDataName(LOCALHOP_ROUTABLE_PREFIXES); |
| 115 | routablePrefixesDataName.appendVersion(); |
| 116 | routablePrefixesDataName.appendSegment(0); |
| 117 | m_routablePrefixesData = make_shared<Data>(routablePrefixesDataName); |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 118 | m_routablePrefixesData->setContent(routablePrefixes.wireEncode()); |
Minsheng Zhang | 60d06d8 | 2015-02-10 14:09:01 -0700 | [diff] [blame] | 119 | m_routablePrefixesData->setFinalBlockId(routablePrefixesDataName.get(-1)); |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 120 | m_routablePrefixesData->setFreshnessPeriod(time::seconds(5)); // 5s |
| 121 | m_keyChain.sign(*m_routablePrefixesData); |
| 122 | } |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 126 | onHubInterest(const Name& name, const Interest& interest) |
| 127 | { |
| 128 | m_face.put(*m_hubData); |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | onRoutablePrefixesInterest(const Name& name, const Interest& interest) |
| 133 | { |
| 134 | m_face.put(*m_routablePrefixesData); |
| 135 | } |
| 136 | |
| 137 | void |
| 138 | onRegisterFailed(const Name& prefix, const std::string& reason) |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 139 | { |
| 140 | std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << |
| 141 | reason << ")" << std::endl; |
| 142 | m_face.shutdown(); |
| 143 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 144 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 145 | void |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 146 | run() |
| 147 | { |
Minsheng Zhang | 60d06d8 | 2015-02-10 14:09:01 -0700 | [diff] [blame] | 148 | m_face.setInterestFilter(LOCALHOP_HUB, |
| 149 | bind(&NdnAutoconfigServer::onHubInterest, this, _1, _2), |
| 150 | RegisterPrefixSuccessCallback(), |
| 151 | bind(&NdnAutoconfigServer::onRegisterFailed, this, _1, _2)); |
| 152 | |
| 153 | if (static_cast<bool>(m_routablePrefixesData)) { |
| 154 | m_face.setInterestFilter(LOCALHOP_ROUTABLE_PREFIXES, |
| 155 | bind(&NdnAutoconfigServer::onRoutablePrefixesInterest, this, _1, _2), |
| 156 | RegisterPrefixSuccessCallback(), |
| 157 | bind(&NdnAutoconfigServer::onRegisterFailed, this, _1, _2)); |
| 158 | } |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 159 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 160 | m_face.processEvents(); |
| 161 | } |
| 162 | |
| 163 | private: |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 164 | Face m_face; |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 165 | |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 166 | shared_ptr<Data> m_hubData; |
| 167 | shared_ptr<Data> m_routablePrefixesData; |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | int |
| 171 | main(int argc, char** argv) |
| 172 | { |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 173 | const char* programName = argv[0]; |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 174 | |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 175 | PrefixCollection routablePrefixes; |
| 176 | |
| 177 | int opt; |
| 178 | while ((opt = getopt(argc, argv, "hVp:")) != -1) { |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 179 | switch (opt) { |
| 180 | case 'h': |
| 181 | usage(programName); |
| 182 | return 0; |
| 183 | case 'V': |
| 184 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 185 | return 0; |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 186 | case 'p': |
| 187 | routablePrefixes.add(Name(optarg)); |
| 188 | break; |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 189 | default: |
| 190 | usage(programName); |
| 191 | return 1; |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 195 | if (argc != optind + 1) { |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 196 | usage(programName); |
| 197 | return 1; |
| 198 | } |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 199 | |
| 200 | std::string hubFaceUri = argv[optind]; |
| 201 | NdnAutoconfigServer instance(hubFaceUri, routablePrefixes); |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 202 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 203 | try { |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 204 | instance.run(); |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 205 | } |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 206 | catch (const std::exception& error) { |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 207 | std::cerr << "ERROR: " << error.what() << std::endl; |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 208 | return 1; |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 209 | } |
| 210 | return 0; |
| 211 | } |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 212 | |
| 213 | } // namespace ndn |
| 214 | |
| 215 | int |
| 216 | main(int argc, char** argv) |
| 217 | { |
| 218 | return ndn::main(argc, argv); |
| 219 | } |