Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Junxiao Shi | fbf7834 | 2015-01-23 14:46:41 -0700 | [diff] [blame] | 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 | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 26 | #include <ndn-cxx/face.hpp> |
| 27 | #include <ndn-cxx/name.hpp> |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 28 | |
Junxiao Shi | 8e273ca | 2014-11-12 00:42:29 -0700 | [diff] [blame] | 29 | #include <ndn-cxx/security/key-chain.hpp> |
Junxiao Shi | a1937bf | 2014-11-06 11:43:40 -0700 | [diff] [blame] | 30 | #include <ndn-cxx/util/face-uri.hpp> |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 31 | #include <ndn-cxx/management/nfd-controller.hpp> |
Alexander Afanasyev | 585e5a6 | 2014-08-12 11:49:31 -0700 | [diff] [blame] | 32 | #include <ndn-cxx/management/nfd-face-monitor.hpp> |
Alexander Afanasyev | f056c11 | 2014-08-14 16:39:25 -0700 | [diff] [blame] | 33 | #include <ndn-cxx/management/nfd-face-status.hpp> |
| 34 | #include <ndn-cxx/encoding/buffer-stream.hpp> |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 35 | |
| 36 | #include <boost/program_options/options_description.hpp> |
| 37 | #include <boost/program_options/variables_map.hpp> |
| 38 | #include <boost/program_options/parsers.hpp> |
| 39 | |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame] | 40 | #include "core/version.hpp" |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 41 | #include "core/network.hpp" |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 42 | |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 43 | using namespace ndn::nfd; |
| 44 | using ndn::Face; |
Junxiao Shi | 8e273ca | 2014-11-12 00:42:29 -0700 | [diff] [blame] | 45 | using ndn::KeyChain; |
Alexander Afanasyev | 585e5a6 | 2014-08-12 11:49:31 -0700 | [diff] [blame] | 46 | using ndn::nfd::FaceEventNotification; |
Junxiao Shi | a1937bf | 2014-11-06 11:43:40 -0700 | [diff] [blame] | 47 | using ndn::util::FaceUri; |
| 48 | using ::nfd::Network; |
| 49 | |
| 50 | namespace ndn { |
| 51 | namespace nfd_autoreg { |
| 52 | |
| 53 | namespace po = boost::program_options; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 54 | |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame] | 55 | class AutoregServer : boost::noncopyable |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 56 | { |
| 57 | public: |
| 58 | AutoregServer() |
Junxiao Shi | 8e273ca | 2014-11-12 00:42:29 -0700 | [diff] [blame] | 59 | : m_controller(m_face, m_keyChain) |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame] | 60 | , m_faceMonitor(m_face) |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 61 | , m_cost(255) |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | void |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 66 | onRegisterCommandSuccess(uint64_t faceId, const Name& prefix) |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 67 | { |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 68 | std::cerr << "SUCCEED: register " << prefix << " on face " << faceId << std::endl; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 72 | onRegisterCommandFailure(uint64_t faceId, const Name& prefix, |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame^] | 73 | const nfd::ControlResponse& response) |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 74 | { |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 75 | std::cerr << "FAILED: register " << prefix << " on face " << faceId |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame^] | 76 | << " (code: " << response.getCode() << ", reason: " << response.getText() << ")" |
| 77 | << std::endl; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame] | 80 | /** |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 81 | * \return true if uri has schema allowed to do auto-registrations |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame] | 82 | */ |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 83 | bool |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 84 | hasAllowedSchema(const FaceUri& uri) |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 85 | { |
| 86 | const std::string& scheme = uri.getScheme(); |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 87 | return (scheme == "udp4" || scheme == "tcp4" || |
| 88 | scheme == "udp6" || scheme == "tcp6"); |
| 89 | } |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 90 | |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 91 | /** |
Alexander Afanasyev | f056c11 | 2014-08-14 16:39:25 -0700 | [diff] [blame] | 92 | * \return true if address is blacklisted |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 93 | */ |
| 94 | bool |
Alexander Afanasyev | f056c11 | 2014-08-14 16:39:25 -0700 | [diff] [blame] | 95 | isBlacklisted(const boost::asio::ip::address& address) |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 96 | { |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 97 | for (std::vector<Network>::const_iterator network = m_blackList.begin(); |
| 98 | network != m_blackList.end(); |
| 99 | ++network) |
| 100 | { |
| 101 | if (network->doesContain(address)) |
| 102 | return true; |
| 103 | } |
| 104 | |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | |
| 108 | /** |
Alexander Afanasyev | f056c11 | 2014-08-14 16:39:25 -0700 | [diff] [blame] | 109 | * \return true if address is whitelisted |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 110 | */ |
| 111 | bool |
Alexander Afanasyev | f056c11 | 2014-08-14 16:39:25 -0700 | [diff] [blame] | 112 | isWhitelisted(const boost::asio::ip::address& address) |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 113 | { |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 114 | for (std::vector<Network>::const_iterator network = m_whiteList.begin(); |
| 115 | network != m_whiteList.end(); |
| 116 | ++network) |
| 117 | { |
| 118 | if (network->doesContain(address)) |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 119 | return true; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 122 | return false; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 126 | registerPrefixesForFace(uint64_t faceId, |
| 127 | const std::vector<ndn::Name>& prefixes) |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 128 | { |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 129 | for (std::vector<ndn::Name>::const_iterator prefix = prefixes.begin(); |
| 130 | prefix != prefixes.end(); |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 131 | ++prefix) |
| 132 | { |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame] | 133 | m_controller.start<RibRegisterCommand>( |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 134 | ControlParameters() |
| 135 | .setName(*prefix) |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 136 | .setFaceId(faceId) |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame] | 137 | .setOrigin(ROUTE_ORIGIN_AUTOREG) |
| 138 | .setCost(m_cost) |
| 139 | .setExpirationPeriod(time::milliseconds::max()), |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 140 | bind(&AutoregServer::onRegisterCommandSuccess, this, faceId, *prefix), |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame^] | 141 | bind(&AutoregServer::onRegisterCommandFailure, this, faceId, *prefix, _1)); |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | void |
Chengyu Fan | 9942cea | 2014-10-13 14:47:13 -0600 | [diff] [blame] | 146 | registerPrefixesIfNeeded(uint64_t faceId, const FaceUri& uri, FacePersistency facePersistency) |
Alexander Afanasyev | f056c11 | 2014-08-14 16:39:25 -0700 | [diff] [blame] | 147 | { |
| 148 | if (hasAllowedSchema(uri)) { |
| 149 | boost::system::error_code ec; |
| 150 | boost::asio::ip::address address = boost::asio::ip::address::from_string(uri.getHost(), ec); |
| 151 | |
| 152 | if (!address.is_multicast()) { |
| 153 | // register all-face prefixes |
| 154 | registerPrefixesForFace(faceId, m_allFacesPrefixes); |
| 155 | |
| 156 | // register autoreg prefixes if new face is on-demand and not blacklisted and whitelisted |
Chengyu Fan | 9942cea | 2014-10-13 14:47:13 -0600 | [diff] [blame] | 157 | if (facePersistency == FACE_PERSISTENCY_ON_DEMAND && |
| 158 | !isBlacklisted(address) && isWhitelisted(address)) { |
Alexander Afanasyev | f056c11 | 2014-08-14 16:39:25 -0700 | [diff] [blame] | 159 | registerPrefixesForFace(faceId, m_autoregPrefixes); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame] | 166 | onNotification(const FaceEventNotification& notification) |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 167 | { |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 168 | if (notification.getKind() == FACE_EVENT_CREATED && |
Chengyu Fan | 9942cea | 2014-10-13 14:47:13 -0600 | [diff] [blame] | 169 | notification.getFaceScope() != FACE_SCOPE_LOCAL) |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 170 | { |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 171 | std::cerr << "PROCESSING: " << notification << std::endl; |
| 172 | |
Alexander Afanasyev | f056c11 | 2014-08-14 16:39:25 -0700 | [diff] [blame] | 173 | registerPrefixesIfNeeded(notification.getFaceId(), FaceUri(notification.getRemoteUri()), |
Chengyu Fan | 9942cea | 2014-10-13 14:47:13 -0600 | [diff] [blame] | 174 | notification.getFacePersistency()); |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 175 | } |
| 176 | else |
| 177 | { |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame] | 178 | std::cerr << "IGNORED: " << notification << std::endl; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 179 | } |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 182 | |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 183 | void |
| 184 | signalHandler() |
| 185 | { |
| 186 | m_face.shutdown(); |
| 187 | } |
| 188 | |
| 189 | |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 190 | void |
Alexander Afanasyev | 60a7ba5 | 2014-03-23 11:23:06 -0700 | [diff] [blame] | 191 | usage(std::ostream& os, |
| 192 | const po::options_description& optionDesciption, |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 193 | const char* programName) |
| 194 | { |
Alexander Afanasyev | 60a7ba5 | 2014-03-23 11:23:06 -0700 | [diff] [blame] | 195 | os << "Usage:\n" |
| 196 | << " " << programName << " --prefix=</autoreg/prefix> [--prefix=/another/prefix] ...\n" |
| 197 | << "\n"; |
| 198 | os << optionDesciption; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void |
| 202 | startProcessing() |
| 203 | { |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame] | 204 | std::cerr << "AUTOREG prefixes: " << std::endl; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 205 | for (std::vector<ndn::Name>::const_iterator prefix = m_autoregPrefixes.begin(); |
| 206 | prefix != m_autoregPrefixes.end(); |
| 207 | ++prefix) |
| 208 | { |
| 209 | std::cout << " " << *prefix << std::endl; |
| 210 | } |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 211 | std::cerr << "ALL-FACES-AUTOREG prefixes: " << std::endl; |
| 212 | for (std::vector<ndn::Name>::const_iterator prefix = m_allFacesPrefixes.begin(); |
| 213 | prefix != m_allFacesPrefixes.end(); |
| 214 | ++prefix) |
| 215 | { |
| 216 | std::cout << " " << *prefix << std::endl; |
| 217 | } |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 218 | |
| 219 | if (!m_blackList.empty()) |
| 220 | { |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame] | 221 | std::cerr << "Blacklisted networks: " << std::endl; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 222 | for (std::vector<Network>::const_iterator network = m_blackList.begin(); |
| 223 | network != m_blackList.end(); |
| 224 | ++network) |
| 225 | { |
| 226 | std::cout << " " << *network << std::endl; |
| 227 | } |
| 228 | } |
| 229 | |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame] | 230 | std::cerr << "Whitelisted networks: " << std::endl; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 231 | for (std::vector<Network>::const_iterator network = m_whiteList.begin(); |
| 232 | network != m_whiteList.end(); |
| 233 | ++network) |
| 234 | { |
| 235 | std::cout << " " << *network << std::endl; |
| 236 | } |
| 237 | |
Junxiao Shi | fbf7834 | 2015-01-23 14:46:41 -0700 | [diff] [blame] | 238 | m_faceMonitor.onNotification.connect(bind(&AutoregServer::onNotification, this, _1)); |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame] | 239 | m_faceMonitor.start(); |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 240 | |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 241 | boost::asio::signal_set signalSet(m_face.getIoService(), SIGINT, SIGTERM); |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 242 | signalSet.async_wait(bind(&AutoregServer::signalHandler, this)); |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 243 | |
| 244 | m_face.processEvents(); |
| 245 | } |
| 246 | |
Alexander Afanasyev | f056c11 | 2014-08-14 16:39:25 -0700 | [diff] [blame] | 247 | |
| 248 | void |
| 249 | fetchFaceStatusSegments(const Data& data, const shared_ptr<ndn::OBufferStream>& buffer) |
| 250 | { |
| 251 | buffer->write(reinterpret_cast<const char*>(data.getContent().value()), |
| 252 | data.getContent().value_size()); |
| 253 | |
| 254 | uint64_t currentSegment = data.getName().get(-1).toSegment(); |
| 255 | |
| 256 | const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId(); |
Junxiao Shi | a1937bf | 2014-11-06 11:43:40 -0700 | [diff] [blame] | 257 | if (finalBlockId.empty() || finalBlockId.toSegment() > currentSegment) { |
| 258 | m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment + 1), |
| 259 | bind(&AutoregServer::fetchFaceStatusSegments, this, _2, buffer), |
| 260 | ndn::OnTimeout()); |
| 261 | } |
| 262 | else { |
| 263 | return processFaceStatusDataset(buffer); |
| 264 | } |
Alexander Afanasyev | f056c11 | 2014-08-14 16:39:25 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void |
| 268 | startFetchingFaceStatusDataset() |
| 269 | { |
| 270 | shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>(); |
| 271 | |
| 272 | Interest interest("/localhost/nfd/faces/list"); |
| 273 | interest.setChildSelector(1); |
| 274 | interest.setMustBeFresh(true); |
| 275 | |
| 276 | m_face.expressInterest(interest, |
| 277 | bind(&AutoregServer::fetchFaceStatusSegments, this, _2, buffer), |
| 278 | ndn::OnTimeout()); |
| 279 | } |
| 280 | |
| 281 | void |
| 282 | processFaceStatusDataset(const shared_ptr<ndn::OBufferStream>& buffer) |
| 283 | { |
| 284 | ndn::ConstBufferPtr buf = buffer->buf(); |
| 285 | std::vector<uint64_t> multicastFaces; |
| 286 | |
| 287 | size_t offset = 0; |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 288 | while (offset < buf->size()) { |
| 289 | bool isOk = false; |
| 290 | Block block; |
| 291 | std::tie(isOk, block) = Block::fromBuffer(buf, offset); |
| 292 | if (!isOk) { |
| 293 | std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl; |
| 294 | break; |
Alexander Afanasyev | f056c11 | 2014-08-14 16:39:25 -0700 | [diff] [blame] | 295 | } |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 296 | |
| 297 | offset += block.size(); |
| 298 | |
| 299 | nfd::FaceStatus faceStatus(block); |
| 300 | registerPrefixesIfNeeded(faceStatus.getFaceId(), FaceUri(faceStatus.getRemoteUri()), |
| 301 | faceStatus.getFacePersistency()); |
| 302 | } |
Alexander Afanasyev | f056c11 | 2014-08-14 16:39:25 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 305 | int |
| 306 | main(int argc, char* argv[]) |
| 307 | { |
| 308 | po::options_description optionDesciption; |
| 309 | optionDesciption.add_options() |
| 310 | ("help,h", "produce help message") |
| 311 | ("prefix,i", po::value<std::vector<ndn::Name> >(&m_autoregPrefixes)->composing(), |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 312 | "prefix that should be automatically registered when new a remote non-local face is " |
| 313 | "established") |
| 314 | ("all-faces-prefix,a", po::value<std::vector<ndn::Name> >(&m_allFacesPrefixes)->composing(), |
| 315 | "prefix that should be automatically registered for all TCP and UDP non-local faces " |
| 316 | "(blacklists and whitelists do not apply to this prefix)") |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 317 | ("cost,c", po::value<uint64_t>(&m_cost)->default_value(255), |
| 318 | "FIB cost which should be assigned to autoreg nexthops") |
| 319 | ("whitelist,w", po::value<std::vector<Network> >(&m_whiteList)->composing(), |
| 320 | "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128") |
| 321 | ("blacklist,b", po::value<std::vector<Network> >(&m_blackList)->composing(), |
| 322 | "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128") |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 323 | ("version,V", "show version and exit") |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 324 | ; |
| 325 | |
| 326 | po::variables_map options; |
| 327 | try |
| 328 | { |
| 329 | po::store(po::command_line_parser(argc, argv).options(optionDesciption).run(), options); |
| 330 | po::notify(options); |
| 331 | } |
| 332 | catch (std::exception& e) |
| 333 | { |
| 334 | std::cerr << "ERROR: " << e.what() << std::endl << std::endl; |
Alexander Afanasyev | 60a7ba5 | 2014-03-23 11:23:06 -0700 | [diff] [blame] | 335 | usage(std::cerr, optionDesciption, argv[0]); |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 336 | return 1; |
| 337 | } |
| 338 | |
| 339 | if (options.count("help")) |
| 340 | { |
Alexander Afanasyev | 60a7ba5 | 2014-03-23 11:23:06 -0700 | [diff] [blame] | 341 | usage(std::cout, optionDesciption, argv[0]); |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 345 | if (options.count("version")) |
| 346 | { |
| 347 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 348 | return 0; |
| 349 | } |
| 350 | |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 351 | if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty()) |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 352 | { |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 353 | std::cerr << "ERROR: at least one --prefix or --all-faces-prefix must be specified" |
| 354 | << std::endl << std::endl; |
Alexander Afanasyev | 60a7ba5 | 2014-03-23 11:23:06 -0700 | [diff] [blame] | 355 | usage(std::cerr, optionDesciption, argv[0]); |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 356 | return 2; |
| 357 | } |
| 358 | |
| 359 | if (m_whiteList.empty()) |
| 360 | { |
| 361 | // Allow everything |
| 362 | m_whiteList.push_back(Network::getMaxRangeV4()); |
| 363 | m_whiteList.push_back(Network::getMaxRangeV6()); |
| 364 | } |
| 365 | |
| 366 | try |
| 367 | { |
Alexander Afanasyev | f056c11 | 2014-08-14 16:39:25 -0700 | [diff] [blame] | 368 | startFetchingFaceStatusDataset(); |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 369 | startProcessing(); |
| 370 | } |
| 371 | catch (std::exception& e) |
| 372 | { |
| 373 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 374 | return 2; |
| 375 | } |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | private: |
| 381 | Face m_face; |
Junxiao Shi | 8e273ca | 2014-11-12 00:42:29 -0700 | [diff] [blame] | 382 | KeyChain m_keyChain; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 383 | Controller m_controller; |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame] | 384 | FaceMonitor m_faceMonitor; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 385 | std::vector<ndn::Name> m_autoregPrefixes; |
Alexander Afanasyev | 81c1a2a | 2014-08-14 16:07:47 -0700 | [diff] [blame] | 386 | std::vector<ndn::Name> m_allFacesPrefixes; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 387 | uint64_t m_cost; |
| 388 | std::vector<Network> m_whiteList; |
| 389 | std::vector<Network> m_blackList; |
| 390 | }; |
| 391 | |
Junxiao Shi | a1937bf | 2014-11-06 11:43:40 -0700 | [diff] [blame] | 392 | } // namespace nfd_autoreg |
| 393 | } // namespace ndn |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 394 | |
| 395 | int |
| 396 | main(int argc, char* argv[]) |
| 397 | { |
Junxiao Shi | a1937bf | 2014-11-06 11:43:40 -0700 | [diff] [blame] | 398 | ndn::nfd_autoreg::AutoregServer server; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 399 | return server.main(argc, argv); |
| 400 | } |