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