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