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 | dda0b46 | 2014-06-30 19:42:29 -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 | * 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 | |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 29 | #include <ndn-cxx/management/nfd-face-event-notification.hpp> |
| 30 | #include <ndn-cxx/management/nfd-controller.hpp> |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 31 | |
| 32 | #include <boost/program_options/options_description.hpp> |
| 33 | #include <boost/program_options/variables_map.hpp> |
| 34 | #include <boost/program_options/parsers.hpp> |
| 35 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 36 | #include "version.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 37 | #include "core/face-uri.hpp" |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 38 | #include "network.hpp" |
| 39 | |
| 40 | namespace po = boost::program_options; |
| 41 | |
| 42 | namespace nfd { |
| 43 | |
| 44 | using namespace ndn::nfd; |
| 45 | using ndn::Face; |
| 46 | |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame^] | 47 | class AutoregServer : boost::noncopyable |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 48 | { |
| 49 | public: |
| 50 | AutoregServer() |
| 51 | : m_controller(m_face) |
| 52 | // , m_lastNotification(<undefined>) |
| 53 | , m_cost(255) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | void |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame^] | 58 | onRegisterCommandSuccess(const FaceEventNotification& notification, const Name& prefix) |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 59 | { |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame^] | 60 | std::cerr << "SUCCEED: register " << prefix << " on face " |
| 61 | << notification.getFaceId() << std::endl; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame^] | 65 | onRegisterCommandFailure(const FaceEventNotification& notification, const Name& prefix, |
| 66 | uint32_t code, const std::string& reason) |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 67 | { |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame^] | 68 | std::cerr << "FAILED: register " << prefix << " on face " << notification.getFaceId() |
Alexander Afanasyev | f4e89b4 | 2014-05-31 15:54:18 +0300 | [diff] [blame] | 69 | << " (code: " << code << ", reason: " << reason << ")" << std::endl; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame^] | 72 | /** |
| 73 | * \return true if auto-register should not be performed for uri |
| 74 | */ |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 75 | bool |
| 76 | isFiltered(const FaceUri& uri) |
| 77 | { |
| 78 | const std::string& scheme = uri.getScheme(); |
| 79 | if (!(scheme == "udp4" || scheme == "tcp4" || |
| 80 | scheme == "udp6" || scheme == "tcp6")) |
| 81 | return true; |
| 82 | |
| 83 | boost::asio::ip::address address = boost::asio::ip::address::from_string(uri.getHost()); |
| 84 | |
| 85 | for (std::vector<Network>::const_iterator network = m_blackList.begin(); |
| 86 | network != m_blackList.end(); |
| 87 | ++network) |
| 88 | { |
| 89 | if (network->doesContain(address)) |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | for (std::vector<Network>::const_iterator network = m_whiteList.begin(); |
| 94 | network != m_whiteList.end(); |
| 95 | ++network) |
| 96 | { |
| 97 | if (network->doesContain(address)) |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | processCreateFace(const FaceEventNotification& notification) |
| 106 | { |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 107 | FaceUri uri(notification.getRemoteUri()); |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 108 | |
| 109 | if (isFiltered(uri)) |
| 110 | { |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame^] | 111 | std::cerr << "FILTERED: " << notification << std::endl; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 112 | return; |
| 113 | } |
| 114 | |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame^] | 115 | std::cerr << "PROCESSING: " << notification << std::endl; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 116 | for (std::vector<ndn::Name>::const_iterator prefix = m_autoregPrefixes.begin(); |
| 117 | prefix != m_autoregPrefixes.end(); |
| 118 | ++prefix) |
| 119 | { |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame^] | 120 | m_controller.start<RibRegisterCommand>( |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 121 | ControlParameters() |
| 122 | .setName(*prefix) |
| 123 | .setFaceId(notification.getFaceId()) |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame^] | 124 | .setOrigin(ROUTE_ORIGIN_AUTOREG) |
| 125 | .setCost(m_cost) |
| 126 | .setExpirationPeriod(time::milliseconds::max()), |
| 127 | bind(&AutoregServer::onRegisterCommandSuccess, this, notification, *prefix), |
| 128 | bind(&AutoregServer::onRegisterCommandFailure, this, notification, *prefix, _1, _2)); |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | onNotification(const Data& data) |
| 134 | { |
| 135 | m_lastNotification = data.getName().get(-1).toSegment(); |
| 136 | |
| 137 | // process |
| 138 | FaceEventNotification notification(data.getContent().blockFromValue()); |
| 139 | |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 140 | if (notification.getKind() == FACE_EVENT_CREATED && |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 141 | !notification.isLocal() && |
| 142 | notification.isOnDemand()) |
| 143 | { |
| 144 | processCreateFace(notification); |
| 145 | } |
| 146 | else |
| 147 | { |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame^] | 148 | std::cerr << "IGNORED: " << notification << std::endl; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | Name nextNotification("/localhost/nfd/faces/events"); |
| 152 | nextNotification |
| 153 | .appendSegment(m_lastNotification + 1); |
| 154 | |
| 155 | // no need to set freshness or child selectors |
| 156 | m_face.expressInterest(nextNotification, |
| 157 | bind(&AutoregServer::onNotification, this, _2), |
| 158 | bind(&AutoregServer::onTimeout, this, _1)); |
| 159 | } |
| 160 | |
| 161 | void |
| 162 | onTimeout(const Interest& timedOutInterest) |
| 163 | { |
| 164 | // re-express the timed out interest, but reset Nonce, since it has to change |
| 165 | |
| 166 | // To be robust against missing notification, use ChildSelector and MustBeFresh |
| 167 | Interest interest("/localhost/nfd/faces/events"); |
| 168 | interest |
| 169 | .setMustBeFresh(true) |
| 170 | .setChildSelector(1) |
| 171 | .setInterestLifetime(time::seconds(60)) |
| 172 | ; |
| 173 | |
| 174 | m_face.expressInterest(interest, |
| 175 | bind(&AutoregServer::onNotification, this, _2), |
| 176 | bind(&AutoregServer::onTimeout, this, _1)); |
| 177 | } |
| 178 | |
| 179 | void |
| 180 | signalHandler() |
| 181 | { |
| 182 | m_face.shutdown(); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | |
| 187 | void |
Alexander Afanasyev | 60a7ba5 | 2014-03-23 11:23:06 -0700 | [diff] [blame] | 188 | usage(std::ostream& os, |
| 189 | const po::options_description& optionDesciption, |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 190 | const char* programName) |
| 191 | { |
Alexander Afanasyev | 60a7ba5 | 2014-03-23 11:23:06 -0700 | [diff] [blame] | 192 | os << "Usage:\n" |
| 193 | << " " << programName << " --prefix=</autoreg/prefix> [--prefix=/another/prefix] ...\n" |
| 194 | << "\n"; |
| 195 | os << optionDesciption; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void |
| 199 | startProcessing() |
| 200 | { |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame^] | 201 | std::cerr << "AUTOREG prefixes: " << std::endl; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 202 | for (std::vector<ndn::Name>::const_iterator prefix = m_autoregPrefixes.begin(); |
| 203 | prefix != m_autoregPrefixes.end(); |
| 204 | ++prefix) |
| 205 | { |
| 206 | std::cout << " " << *prefix << std::endl; |
| 207 | } |
| 208 | |
| 209 | if (!m_blackList.empty()) |
| 210 | { |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame^] | 211 | std::cerr << "Blacklisted networks: " << std::endl; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 212 | for (std::vector<Network>::const_iterator network = m_blackList.begin(); |
| 213 | network != m_blackList.end(); |
| 214 | ++network) |
| 215 | { |
| 216 | std::cout << " " << *network << std::endl; |
| 217 | } |
| 218 | } |
| 219 | |
Junxiao Shi | dda0b46 | 2014-06-30 19:42:29 -0700 | [diff] [blame^] | 220 | std::cerr << "Whitelisted networks: " << std::endl; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 221 | for (std::vector<Network>::const_iterator network = m_whiteList.begin(); |
| 222 | network != m_whiteList.end(); |
| 223 | ++network) |
| 224 | { |
| 225 | std::cout << " " << *network << std::endl; |
| 226 | } |
| 227 | |
| 228 | Interest interest("/localhost/nfd/faces/events"); |
| 229 | interest |
| 230 | .setMustBeFresh(true) |
| 231 | .setChildSelector(1) |
| 232 | .setInterestLifetime(time::seconds(60)) |
| 233 | ; |
| 234 | |
| 235 | m_face.expressInterest(interest, |
| 236 | bind(&AutoregServer::onNotification, this, _2), |
| 237 | bind(&AutoregServer::onTimeout, this, _1)); |
| 238 | |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 239 | boost::asio::signal_set signalSet(m_face.getIoService(), SIGINT, SIGTERM); |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 240 | signalSet.async_wait(bind(&AutoregServer::signalHandler, this)); |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 241 | |
| 242 | m_face.processEvents(); |
| 243 | } |
| 244 | |
| 245 | int |
| 246 | main(int argc, char* argv[]) |
| 247 | { |
| 248 | po::options_description optionDesciption; |
| 249 | optionDesciption.add_options() |
| 250 | ("help,h", "produce help message") |
| 251 | ("prefix,i", po::value<std::vector<ndn::Name> >(&m_autoregPrefixes)->composing(), |
| 252 | "prefix that should be automatically registered when new remote face is established") |
| 253 | ("cost,c", po::value<uint64_t>(&m_cost)->default_value(255), |
| 254 | "FIB cost which should be assigned to autoreg nexthops") |
| 255 | ("whitelist,w", po::value<std::vector<Network> >(&m_whiteList)->composing(), |
| 256 | "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128") |
| 257 | ("blacklist,b", po::value<std::vector<Network> >(&m_blackList)->composing(), |
| 258 | "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128") |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 259 | ("version,V", "show version and exit") |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 260 | ; |
| 261 | |
| 262 | po::variables_map options; |
| 263 | try |
| 264 | { |
| 265 | po::store(po::command_line_parser(argc, argv).options(optionDesciption).run(), options); |
| 266 | po::notify(options); |
| 267 | } |
| 268 | catch (std::exception& e) |
| 269 | { |
| 270 | std::cerr << "ERROR: " << e.what() << std::endl << std::endl; |
Alexander Afanasyev | 60a7ba5 | 2014-03-23 11:23:06 -0700 | [diff] [blame] | 271 | usage(std::cerr, optionDesciption, argv[0]); |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 272 | return 1; |
| 273 | } |
| 274 | |
| 275 | if (options.count("help")) |
| 276 | { |
Alexander Afanasyev | 60a7ba5 | 2014-03-23 11:23:06 -0700 | [diff] [blame] | 277 | usage(std::cout, optionDesciption, argv[0]); |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 281 | if (options.count("version")) |
| 282 | { |
| 283 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 284 | return 0; |
| 285 | } |
| 286 | |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 287 | if (m_autoregPrefixes.empty()) |
| 288 | { |
| 289 | std::cerr << "ERROR: at least one --prefix must be specified" << std::endl << std::endl; |
Alexander Afanasyev | 60a7ba5 | 2014-03-23 11:23:06 -0700 | [diff] [blame] | 290 | usage(std::cerr, optionDesciption, argv[0]); |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 291 | return 2; |
| 292 | } |
| 293 | |
| 294 | if (m_whiteList.empty()) |
| 295 | { |
| 296 | // Allow everything |
| 297 | m_whiteList.push_back(Network::getMaxRangeV4()); |
| 298 | m_whiteList.push_back(Network::getMaxRangeV6()); |
| 299 | } |
| 300 | |
| 301 | try |
| 302 | { |
| 303 | startProcessing(); |
| 304 | } |
| 305 | catch (std::exception& e) |
| 306 | { |
| 307 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 308 | return 2; |
| 309 | } |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | private: |
| 315 | Face m_face; |
| 316 | Controller m_controller; |
| 317 | uint64_t m_lastNotification; |
| 318 | std::vector<ndn::Name> m_autoregPrefixes; |
| 319 | uint64_t m_cost; |
| 320 | std::vector<Network> m_whiteList; |
| 321 | std::vector<Network> m_blackList; |
| 322 | }; |
| 323 | |
| 324 | } // namespace nfd |
| 325 | |
| 326 | int |
| 327 | main(int argc, char* argv[]) |
| 328 | { |
| 329 | nfd::AutoregServer server; |
| 330 | return server.main(argc, argv); |
| 331 | } |