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