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