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