Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "nrd.hpp" |
| 8 | |
| 9 | namespace ndn { |
| 10 | namespace nrd { |
| 11 | |
| 12 | const Name Nrd::COMMAND_PREFIX = "/localhost/nrd"; |
Obaid | ea56c61 | 2014-03-17 22:50:47 -0500 | [diff] [blame] | 13 | const Name Nrd::REMOTE_COMMAND_PREFIX = "/localhop/nrd"; |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 14 | |
| 15 | const size_t Nrd::COMMAND_UNSIGNED_NCOMPS = |
| 16 | Nrd::COMMAND_PREFIX.size() + |
| 17 | 1 + // verb |
| 18 | 1; // verb options |
| 19 | |
| 20 | const size_t Nrd::COMMAND_SIGNED_NCOMPS = |
| 21 | Nrd::COMMAND_UNSIGNED_NCOMPS + |
| 22 | 4; // (timestamp, nonce, signed info tlv, signature tlv) |
| 23 | |
| 24 | const Nrd::VerbAndProcessor Nrd::COMMAND_VERBS[] = |
| 25 | { |
| 26 | VerbAndProcessor( |
| 27 | Name::Component("register"), |
| 28 | &Nrd::insertEntry |
| 29 | ), |
| 30 | |
| 31 | VerbAndProcessor( |
| 32 | Name::Component("unregister"), |
| 33 | &Nrd::deleteEntry |
| 34 | ), |
| 35 | }; |
| 36 | |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 37 | Nrd::Nrd(const std::string& validatorConfig) |
| 38 | : m_face(new Face()) |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 39 | , m_nfdController(new nfd::Controller(*m_face)) |
Obaid | 6871187 | 2014-04-08 03:16:40 -0500 | [diff] [blame^] | 40 | , m_validator(m_face) |
| 41 | , m_faceMonitor(*m_face) |
Alexander Afanasyev | d9e9873 | 2014-03-27 16:09:10 -0700 | [diff] [blame] | 42 | , m_verbDispatch(COMMAND_VERBS, |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 43 | COMMAND_VERBS + (sizeof(COMMAND_VERBS) / sizeof(VerbAndProcessor))) |
| 44 | { |
Obaid | ea56c61 | 2014-03-17 22:50:47 -0500 | [diff] [blame] | 45 | //check whether the components of localhop and localhost prefixes are same |
Alexander Afanasyev | 80f06a5 | 2014-03-27 14:40:00 -0700 | [diff] [blame] | 46 | BOOST_ASSERT(COMMAND_PREFIX.size() == REMOTE_COMMAND_PREFIX.size()); |
Obaid | ea56c61 | 2014-03-17 22:50:47 -0500 | [diff] [blame] | 47 | |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 48 | m_validator.load(validatorConfig); |
| 49 | |
Obaid | ea56c61 | 2014-03-17 22:50:47 -0500 | [diff] [blame] | 50 | std::cerr << "Setting interest filter on: " << COMMAND_PREFIX.toUri() << std::endl; |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 51 | m_face->setController(m_nfdController); |
| 52 | m_face->setInterestFilter(COMMAND_PREFIX.toUri(), |
| 53 | bind(&Nrd::onRibRequest, this, _2), |
| 54 | bind(&Nrd::setInterestFilterFailed, this, _1, _2)); |
Alexander Afanasyev | 80f06a5 | 2014-03-27 14:40:00 -0700 | [diff] [blame] | 55 | |
Obaid | ea56c61 | 2014-03-17 22:50:47 -0500 | [diff] [blame] | 56 | std::cerr << "Setting interest filter on: " << REMOTE_COMMAND_PREFIX.toUri() << std::endl; |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 57 | m_face->setInterestFilter(REMOTE_COMMAND_PREFIX.toUri(), |
| 58 | bind(&Nrd::onRibRequest, this, _2), |
| 59 | bind(&Nrd::setInterestFilterFailed, this, _1, _2)); |
Obaid | 6871187 | 2014-04-08 03:16:40 -0500 | [diff] [blame^] | 60 | |
| 61 | std::cerr << "Monitoring faces" << std::endl; |
| 62 | m_faceMonitor.addSubscriber(boost::bind(&Nrd::onNotification, this, _1)); |
| 63 | m_faceMonitor.startNotifications(); |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void |
Obaid | 6871187 | 2014-04-08 03:16:40 -0500 | [diff] [blame^] | 67 | Nrd::setInterestFilterFailed(const Name& name, const std::string& msg) |
| 68 | { |
| 69 | std::cerr << "Error in setting interest filter (" << name << "): " << msg << std::endl; |
| 70 | m_face->shutdown(); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | void |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 75 | Nrd::sendResponse(const Name& name, |
| 76 | const nfd::ControlResponse& response) |
| 77 | { |
| 78 | const Block& encodedControl = response.wireEncode(); |
| 79 | |
| 80 | Data responseData(name); |
| 81 | responseData.setContent(encodedControl); |
| 82 | |
| 83 | m_keyChain.sign(responseData); |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 84 | m_face->put(responseData); |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
| 88 | Nrd::sendResponse(const Name& name, |
| 89 | uint32_t code, |
| 90 | const std::string& text) |
| 91 | { |
| 92 | nfd::ControlResponse response(code, text); |
| 93 | sendResponse(name, response); |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | Nrd::onRibRequest(const Interest& request) |
| 98 | { |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 99 | m_validator.validate(request, |
| 100 | bind(&Nrd::onRibRequestValidated, this, _1), |
| 101 | bind(&Nrd::onRibRequestValidationFailed, this, _1, _2)); |
| 102 | } |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 103 | |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 104 | void |
| 105 | Nrd::onRibRequestValidated(const shared_ptr<const Interest>& request) |
| 106 | { |
| 107 | const Name& command = request->getName(); |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 108 | |
Alexander Afanasyev | 80f06a5 | 2014-03-27 14:40:00 -0700 | [diff] [blame] | 109 | //REMOTE_COMMAND_PREFIX number of componenets are same as |
Obaid | ea56c61 | 2014-03-17 22:50:47 -0500 | [diff] [blame] | 110 | // NRD_COMMAND_PREFIX's so no extra checks are required. |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 111 | const Name::Component& verb = command.get(COMMAND_PREFIX.size()); |
| 112 | VerbDispatchTable::const_iterator verbProcessor = m_verbDispatch.find(verb); |
| 113 | |
| 114 | if (verbProcessor != m_verbDispatch.end()) |
| 115 | { |
| 116 | PrefixRegOptions options; |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 117 | if (!extractOptions(*request, options)) |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 118 | { |
| 119 | sendResponse(command, 400, "Malformed command"); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | /// \todo authorize command |
| 124 | if (false) |
| 125 | { |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 126 | sendResponse(request->getName(), 403, "Unauthorized command"); |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 127 | return; |
| 128 | } |
| 129 | |
| 130 | // \todo add proper log support |
Alexander Afanasyev | 80f06a5 | 2014-03-27 14:40:00 -0700 | [diff] [blame] | 131 | std::cout << "Received options (name, faceid, cost): " << options.getName() << |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 132 | ", " << options.getFaceId() << ", " << options.getCost() << std::endl; |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 133 | |
| 134 | nfd::ControlResponse response; |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 135 | (verbProcessor->second)(this, *request, options); |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 136 | } |
| 137 | else |
| 138 | { |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 139 | sendResponse(request->getName(), 501, "Unsupported command"); |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 143 | void |
| 144 | Nrd::onRibRequestValidationFailed(const shared_ptr<const Interest>& request, |
| 145 | const std::string& failureInfo) |
| 146 | { |
| 147 | sendResponse(request->getName(), 403, failureInfo); |
| 148 | } |
| 149 | |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 150 | bool |
| 151 | Nrd::extractOptions(const Interest& request, |
| 152 | PrefixRegOptions& extractedOptions) |
| 153 | { |
Alexander Afanasyev | 80f06a5 | 2014-03-27 14:40:00 -0700 | [diff] [blame] | 154 | // const Name& command = request.getName(); |
| 155 | //REMOTE_COMMAND_PREFIX is same in size of NRD_COMMAND_PREFIX |
Obaid | ea56c61 | 2014-03-17 22:50:47 -0500 | [diff] [blame] | 156 | //so no extra processing is required. |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 157 | const size_t optionCompIndex = COMMAND_PREFIX.size() + 1; |
| 158 | |
| 159 | try |
| 160 | { |
| 161 | Block rawOptions = request.getName()[optionCompIndex].blockFromValue(); |
| 162 | extractedOptions.wireDecode(rawOptions); |
| 163 | } |
| 164 | catch (const ndn::Tlv::Error& e) |
| 165 | { |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | if (extractedOptions.getFaceId() == 0) |
| 170 | { |
| 171 | std::cout <<"IncomingFaceId: " << request.getIncomingFaceId() << std::endl; |
| 172 | extractedOptions.setFaceId(request.getIncomingFaceId()); |
| 173 | } |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | void |
Alexander Afanasyev | d9e9873 | 2014-03-27 16:09:10 -0700 | [diff] [blame] | 178 | Nrd::onCommandError(uint32_t code, const std::string& error, |
Alexander Afanasyev | 80f06a5 | 2014-03-27 14:40:00 -0700 | [diff] [blame] | 179 | const Interest& request, |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 180 | const PrefixRegOptions& options) |
| 181 | { |
Alexander Afanasyev | d9e9873 | 2014-03-27 16:09:10 -0700 | [diff] [blame] | 182 | std::cout << "NFD Error: " << error << " (code: " << code << ")" << std::endl; |
| 183 | |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 184 | nfd::ControlResponse response; |
Alexander Afanasyev | 80f06a5 | 2014-03-27 14:40:00 -0700 | [diff] [blame] | 185 | |
Alexander Afanasyev | d9e9873 | 2014-03-27 16:09:10 -0700 | [diff] [blame] | 186 | if (code == 404) |
| 187 | { |
| 188 | response.setCode(code); |
| 189 | response.setText(error); |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | response.setCode(533); |
| 194 | std::ostringstream os; |
| 195 | os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")"; |
| 196 | response.setText(os.str()); |
| 197 | } |
Alexander Afanasyev | 80f06a5 | 2014-03-27 14:40:00 -0700 | [diff] [blame] | 198 | |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 199 | sendResponse(request.getName(), response); |
| 200 | m_managedRib.erase(options); |
| 201 | } |
| 202 | |
| 203 | void |
| 204 | Nrd::onUnRegSuccess(const Interest& request, const PrefixRegOptions& options) |
| 205 | { |
| 206 | nfd::ControlResponse response; |
Alexander Afanasyev | 80f06a5 | 2014-03-27 14:40:00 -0700 | [diff] [blame] | 207 | |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 208 | response.setCode(200); |
| 209 | response.setText("Success"); |
| 210 | response.setBody(options.wireEncode()); |
Alexander Afanasyev | 80f06a5 | 2014-03-27 14:40:00 -0700 | [diff] [blame] | 211 | |
| 212 | std::cout << "Success: Name unregistered (" << |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 213 | options.getName() << ", " << |
| 214 | options.getFaceId() << ")" << std::endl; |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 215 | sendResponse(request.getName(), response); |
| 216 | m_managedRib.erase(options); |
| 217 | } |
| 218 | |
| 219 | void |
| 220 | Nrd::onRegSuccess(const Interest& request, const PrefixRegOptions& options) |
| 221 | { |
| 222 | nfd::ControlResponse response; |
Alexander Afanasyev | 80f06a5 | 2014-03-27 14:40:00 -0700 | [diff] [blame] | 223 | |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 224 | response.setCode(200); |
| 225 | response.setText("Success"); |
| 226 | response.setBody(options.wireEncode()); |
Alexander Afanasyev | 80f06a5 | 2014-03-27 14:40:00 -0700 | [diff] [blame] | 227 | |
| 228 | std::cout << "Success: Name registered (" << options.getName() << ", " << |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 229 | options.getFaceId() << ")" << std::endl; |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 230 | sendResponse(request.getName(), response); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | void |
| 235 | Nrd::insertEntry(const Interest& request, const PrefixRegOptions& options) |
| 236 | { |
| 237 | // For right now, just pass the options to fib as it is, |
| 238 | // without processing flags. Later options will be first added to |
| 239 | // Rib tree, then nrd will generate fib updates based on flags and then |
| 240 | // will add next hops one by one.. |
| 241 | m_managedRib.insert(options); |
Alexander Afanasyev | d9e9873 | 2014-03-27 16:09:10 -0700 | [diff] [blame] | 242 | m_nfdController->start<nfd::FibAddNextHopCommand>( |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 243 | nfd::ControlParameters() |
| 244 | .setName(options.getName()) |
| 245 | .setFaceId(options.getFaceId()) |
| 246 | .setCost(options.getCost()), |
| 247 | bind(&Nrd::onRegSuccess, this, request, options), |
| 248 | bind(&Nrd::onCommandError, this, _1, _2, request, options)); |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | |
| 252 | void |
| 253 | Nrd::deleteEntry(const Interest& request, const PrefixRegOptions& options) |
| 254 | { |
Alexander Afanasyev | d9e9873 | 2014-03-27 16:09:10 -0700 | [diff] [blame] | 255 | m_nfdController->start<nfd::FibRemoveNextHopCommand>( |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 256 | nfd::ControlParameters() |
| 257 | .setName(options.getName()) |
| 258 | .setFaceId(options.getFaceId()), |
| 259 | bind(&Nrd::onUnRegSuccess, this, request, options), |
| 260 | bind(&Nrd::onCommandError, this, _1, _2, request, options)); |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | |
| 264 | void |
| 265 | Nrd::listen() |
| 266 | { |
| 267 | std::cout << "NRD started: listening for incoming interests" << std::endl; |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 268 | m_face->processEvents(); |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | |
| 272 | void |
| 273 | Nrd::onControlHeaderSuccess() |
| 274 | { |
| 275 | std::cout << "Local control header enabled" << std::endl; |
| 276 | } |
| 277 | |
| 278 | |
| 279 | void |
Alexander Afanasyev | d9e9873 | 2014-03-27 16:09:10 -0700 | [diff] [blame] | 280 | Nrd::onControlHeaderError(uint32_t code, const std::string& reason) |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 281 | { |
Alexander Afanasyev | d9e9873 | 2014-03-27 16:09:10 -0700 | [diff] [blame] | 282 | std::cout << "Error: couldn't enable local control header " |
| 283 | << "(code: " << code << ", info: " << reason << ")" << std::endl; |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 284 | m_face->shutdown(); |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 285 | } |
| 286 | |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 287 | void |
| 288 | Nrd::enableLocalControlHeader() |
| 289 | { |
Alexander Afanasyev | d9e9873 | 2014-03-27 16:09:10 -0700 | [diff] [blame] | 290 | m_nfdController->start<nfd::FaceEnableLocalControlCommand>( |
| 291 | nfd::ControlParameters() |
| 292 | .setLocalControlFeature(nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID), |
| 293 | bind(&Nrd::onControlHeaderSuccess, this), |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 294 | bind(&Nrd::onControlHeaderError, this, _1, _2)); |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 295 | } |
| 296 | |
Obaid | 6871187 | 2014-04-08 03:16:40 -0500 | [diff] [blame^] | 297 | void |
| 298 | Nrd::onNotification(const nfd::FaceEventNotification& notification) |
| 299 | { |
| 300 | /// \todo A notification can be missed, in this case check Facelist |
| 301 | if (notification.getKind() == 2) { //face destroyed |
| 302 | m_managedRib.erase(notification.getFaceId()); |
| 303 | } |
| 304 | } |
| 305 | |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 306 | } // namespace nrd |
| 307 | } // namespace ndn |