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 LOCALHOP_HUB = "/localhop/ndn-autoconf/hub"; |
Minsheng Zhang | 60d06d8 | 2015-02-10 14:09:01 -0700 | [diff] [blame] | 32 | const static Name LOCALHOP_ROUTABLE_PREFIXES = "/localhop/nfd/rib/routable-prefixes"; |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 33 | |
| 34 | static void |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 35 | usage(const char* programName) |
| 36 | { |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 37 | std::cout << "Usage:\n" << programName << " [-h] [-V] [-p prefix] [-p prefix] ... Uri \n" |
| 38 | << " -h - print usage and exit\n" |
| 39 | << " -V - print version number and exit\n" |
| 40 | << " -p prefix - the local prefix of the hub\n" |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 41 | << "\n" |
| 42 | << " Uri - a FaceMgmt URI\n" |
| 43 | << std::endl; |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 44 | } |
| 45 | |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 46 | class PrefixCollection : noncopyable |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 47 | { |
| 48 | public: |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 49 | bool |
| 50 | empty() const |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 51 | { |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 52 | return m_prefixes.empty(); |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 53 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 54 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 55 | void |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 56 | add(const Name& prefix) |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 57 | { |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 58 | m_prefixes.push_back(prefix); |
| 59 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 60 | |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 61 | template<bool T> |
| 62 | size_t |
| 63 | wireEncode(EncodingImpl<T>& encoder) const |
| 64 | { |
| 65 | size_t totalLength = 0; |
| 66 | |
| 67 | for (std::vector<Name>::const_reverse_iterator i = m_prefixes.rbegin(); |
| 68 | i != m_prefixes.rend(); ++i) { |
| 69 | totalLength += i->wireEncode(encoder); |
| 70 | } |
| 71 | |
| 72 | totalLength += encoder.prependVarNumber(totalLength); |
| 73 | totalLength += encoder.prependVarNumber(tlv::Content); |
| 74 | return totalLength; |
| 75 | } |
| 76 | |
| 77 | Block |
| 78 | wireEncode() const |
| 79 | { |
| 80 | Block block; |
| 81 | |
| 82 | EncodingEstimator estimator; |
| 83 | size_t estimatedSize = wireEncode(estimator); |
| 84 | |
| 85 | EncodingBuffer buffer(estimatedSize); |
| 86 | wireEncode(buffer); |
| 87 | |
| 88 | return buffer.block(); |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | std::vector<Name> m_prefixes; |
| 93 | }; |
| 94 | |
| 95 | class NdnAutoconfigServer : noncopyable |
| 96 | { |
| 97 | public: |
| 98 | NdnAutoconfigServer(const std::string& hubFaceUri, const PrefixCollection& routablePrefixes) |
| 99 | { |
| 100 | KeyChain m_keyChain; |
| 101 | |
| 102 | // pre-create hub Data |
| 103 | m_hubData = make_shared<Data>(Name(LOCALHOP_HUB).appendVersion()); |
| 104 | m_hubData->setFreshnessPeriod(time::hours(1)); // 1 hour |
| 105 | m_hubData->setContent(dataBlock(tlv::nfd::Uri, |
| 106 | reinterpret_cast<const uint8_t*>(hubFaceUri.c_str()), |
| 107 | hubFaceUri.size())); |
| 108 | m_keyChain.sign(*m_hubData); |
| 109 | |
| 110 | // pre-create routable prefix Data |
| 111 | if (!routablePrefixes.empty()) { |
Minsheng Zhang | 60d06d8 | 2015-02-10 14:09:01 -0700 | [diff] [blame] | 112 | Name routablePrefixesDataName(LOCALHOP_ROUTABLE_PREFIXES); |
| 113 | routablePrefixesDataName.appendVersion(); |
| 114 | routablePrefixesDataName.appendSegment(0); |
| 115 | m_routablePrefixesData = make_shared<Data>(routablePrefixesDataName); |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 116 | m_routablePrefixesData->setContent(routablePrefixes.wireEncode()); |
Minsheng Zhang | 60d06d8 | 2015-02-10 14:09:01 -0700 | [diff] [blame] | 117 | m_routablePrefixesData->setFinalBlockId(routablePrefixesDataName.get(-1)); |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 118 | m_routablePrefixesData->setFreshnessPeriod(time::seconds(5)); // 5s |
| 119 | m_keyChain.sign(*m_routablePrefixesData); |
| 120 | } |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 124 | onHubInterest(const Name& name, const Interest& interest) |
| 125 | { |
| 126 | m_face.put(*m_hubData); |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | onRoutablePrefixesInterest(const Name& name, const Interest& interest) |
| 131 | { |
| 132 | m_face.put(*m_routablePrefixesData); |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | onRegisterFailed(const Name& prefix, const std::string& reason) |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 137 | { |
| 138 | std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << |
| 139 | reason << ")" << std::endl; |
| 140 | m_face.shutdown(); |
| 141 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 142 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 143 | void |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 144 | run() |
| 145 | { |
Minsheng Zhang | 60d06d8 | 2015-02-10 14:09:01 -0700 | [diff] [blame] | 146 | m_face.setInterestFilter(LOCALHOP_HUB, |
| 147 | bind(&NdnAutoconfigServer::onHubInterest, this, _1, _2), |
| 148 | RegisterPrefixSuccessCallback(), |
| 149 | bind(&NdnAutoconfigServer::onRegisterFailed, this, _1, _2)); |
| 150 | |
| 151 | if (static_cast<bool>(m_routablePrefixesData)) { |
| 152 | m_face.setInterestFilter(LOCALHOP_ROUTABLE_PREFIXES, |
| 153 | bind(&NdnAutoconfigServer::onRoutablePrefixesInterest, this, _1, _2), |
| 154 | RegisterPrefixSuccessCallback(), |
| 155 | bind(&NdnAutoconfigServer::onRegisterFailed, this, _1, _2)); |
| 156 | } |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 157 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 158 | m_face.processEvents(); |
| 159 | } |
| 160 | |
| 161 | private: |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 162 | Face m_face; |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 163 | |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 164 | shared_ptr<Data> m_hubData; |
| 165 | shared_ptr<Data> m_routablePrefixesData; |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | int |
| 169 | main(int argc, char** argv) |
| 170 | { |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 171 | const char* programName = argv[0]; |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 172 | |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 173 | PrefixCollection routablePrefixes; |
| 174 | |
| 175 | int opt; |
| 176 | while ((opt = getopt(argc, argv, "hVp:")) != -1) { |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 177 | switch (opt) { |
| 178 | case 'h': |
| 179 | usage(programName); |
| 180 | return 0; |
| 181 | case 'V': |
| 182 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 183 | return 0; |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 184 | case 'p': |
| 185 | routablePrefixes.add(Name(optarg)); |
| 186 | break; |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 187 | default: |
| 188 | usage(programName); |
| 189 | return 1; |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 193 | if (argc != optind + 1) { |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 194 | usage(programName); |
| 195 | return 1; |
| 196 | } |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 197 | |
| 198 | std::string hubFaceUri = argv[optind]; |
| 199 | NdnAutoconfigServer instance(hubFaceUri, routablePrefixes); |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 200 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 201 | try { |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 202 | instance.run(); |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 203 | } |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 204 | catch (const std::exception& error) { |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 205 | std::cerr << "ERROR: " << error.what() << std::endl; |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 206 | return 1; |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 207 | } |
| 208 | return 0; |
| 209 | } |
Yingdi Yu | 7b0e9cf | 2014-08-29 14:57:32 -0700 | [diff] [blame] | 210 | |
| 211 | } // namespace ndn |
| 212 | |
| 213 | int |
| 214 | main(int argc, char** argv) |
| 215 | { |
| 216 | return ndn::main(argc, argv); |
| 217 | } |