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