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