Steve DiBenedetto | 471c060 | 2014-02-18 12:32:00 -0700 | [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 "local-control-header-manager.hpp" |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 8 | #include "face/local-face.hpp" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 9 | #include "mgmt/internal-face.hpp" |
Steve DiBenedetto | 471c060 | 2014-02-18 12:32:00 -0700 | [diff] [blame] | 10 | |
| 11 | namespace nfd { |
| 12 | |
| 13 | NFD_LOG_INIT("LocalControlHeaderManager"); |
| 14 | |
| 15 | const Name LocalControlHeaderManager::COMMAND_PREFIX = "/localhost/nfd/control-header"; |
| 16 | |
| 17 | const size_t LocalControlHeaderManager::COMMAND_UNSIGNED_NCOMPS = |
| 18 | LocalControlHeaderManager::COMMAND_PREFIX.size() + |
| 19 | 1 + // control-module |
| 20 | 1; // verb |
| 21 | |
| 22 | const size_t LocalControlHeaderManager::COMMAND_SIGNED_NCOMPS = |
| 23 | LocalControlHeaderManager::COMMAND_UNSIGNED_NCOMPS + |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 24 | 4; // (timestamp, nonce, signed info tlv, signature tlv) |
Steve DiBenedetto | 471c060 | 2014-02-18 12:32:00 -0700 | [diff] [blame] | 25 | |
| 26 | |
| 27 | LocalControlHeaderManager::LocalControlHeaderManager(function<shared_ptr<Face>(FaceId)> getFace, |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 28 | shared_ptr<InternalFace> face) |
| 29 | : ManagerBase(face, CONTROL_HEADER_PRIVILEGE), |
Steve DiBenedetto | 471c060 | 2014-02-18 12:32:00 -0700 | [diff] [blame] | 30 | m_getFace(getFace) |
| 31 | { |
| 32 | face->setInterestFilter("/localhost/nfd/control-header", |
| 33 | bind(&LocalControlHeaderManager::onLocalControlHeaderRequest, this, _2)); |
| 34 | } |
| 35 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 36 | |
| 37 | |
Steve DiBenedetto | 471c060 | 2014-02-18 12:32:00 -0700 | [diff] [blame] | 38 | void |
| 39 | LocalControlHeaderManager::onLocalControlHeaderRequest(const Interest& request) |
| 40 | { |
Steve DiBenedetto | 471c060 | 2014-02-18 12:32:00 -0700 | [diff] [blame] | 41 | const Name& command = request.getName(); |
| 42 | const size_t commandNComps = command.size(); |
| 43 | |
| 44 | if (COMMAND_UNSIGNED_NCOMPS <= commandNComps && |
| 45 | commandNComps < COMMAND_SIGNED_NCOMPS) |
| 46 | { |
| 47 | NFD_LOG_INFO("command result: unsigned verb: " << command); |
| 48 | sendResponse(command, 401, "Signature required"); |
| 49 | |
| 50 | return; |
| 51 | } |
| 52 | else if (commandNComps < COMMAND_SIGNED_NCOMPS || |
| 53 | !COMMAND_PREFIX.isPrefixOf(command)) |
| 54 | { |
| 55 | NFD_LOG_INFO("command result: malformed"); |
| 56 | sendResponse(command, 400, "Malformed command"); |
| 57 | return; |
| 58 | } |
| 59 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 60 | validate(request, |
| 61 | bind(&LocalControlHeaderManager::onCommandValidated, |
| 62 | this, _1), |
| 63 | bind(&ManagerBase::onCommandValidationFailed, |
| 64 | this, _1, _2)); |
| 65 | |
| 66 | |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | LocalControlHeaderManager::onCommandValidated(const shared_ptr<const Interest>& command) |
| 71 | { |
| 72 | static const Name::Component MODULE_IN_FACEID("in-faceid"); |
| 73 | static const Name::Component MODULE_NEXTHOP_FACEID("nexthop-faceid"); |
| 74 | static const Name::Component VERB_ENABLE("enable"); |
| 75 | static const Name::Component VERB_DISABLE("disable"); |
| 76 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 77 | shared_ptr<LocalFace> face = |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 78 | dynamic_pointer_cast<LocalFace>(m_getFace(command->getIncomingFaceId())); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 79 | |
| 80 | if (!static_cast<bool>(face)) |
| 81 | { |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 82 | NFD_LOG_INFO("command result: command to enable control header on non-local face"); |
| 83 | sendResponse(command->getName(), 400, "Command not supported on the requested face"); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 84 | return; |
| 85 | } |
Steve DiBenedetto | 471c060 | 2014-02-18 12:32:00 -0700 | [diff] [blame] | 86 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 87 | const Name& commandName = command->getName(); |
| 88 | const Name::Component& module = commandName[COMMAND_PREFIX.size()]; |
| 89 | const Name::Component& verb = commandName[COMMAND_PREFIX.size() + 1]; |
Steve DiBenedetto | 471c060 | 2014-02-18 12:32:00 -0700 | [diff] [blame] | 90 | |
| 91 | if (module == MODULE_IN_FACEID) |
| 92 | { |
| 93 | if (verb == VERB_ENABLE) |
| 94 | { |
| 95 | face->setLocalControlHeaderFeature(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID, true); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 96 | sendResponse(commandName, 200, "Success"); |
Steve DiBenedetto | 471c060 | 2014-02-18 12:32:00 -0700 | [diff] [blame] | 97 | } |
| 98 | else if (verb == VERB_DISABLE) |
| 99 | { |
| 100 | face->setLocalControlHeaderFeature(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID, false); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 101 | sendResponse(commandName, 200, "Success"); |
Steve DiBenedetto | 471c060 | 2014-02-18 12:32:00 -0700 | [diff] [blame] | 102 | } |
| 103 | else |
| 104 | { |
| 105 | NFD_LOG_INFO("command result: unsupported verb: " << verb); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 106 | sendResponse(commandName, 501, "Unsupported"); |
Steve DiBenedetto | 471c060 | 2014-02-18 12:32:00 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | else if (module == MODULE_NEXTHOP_FACEID) |
| 110 | { |
| 111 | if (verb == VERB_ENABLE) |
| 112 | { |
| 113 | face->setLocalControlHeaderFeature(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID, true); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 114 | sendResponse(commandName, 200, "Success"); |
Steve DiBenedetto | 471c060 | 2014-02-18 12:32:00 -0700 | [diff] [blame] | 115 | } |
| 116 | else if (verb == VERB_DISABLE) |
| 117 | { |
| 118 | face->setLocalControlHeaderFeature(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID, false); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 119 | sendResponse(commandName, 200, "Success"); |
Steve DiBenedetto | 471c060 | 2014-02-18 12:32:00 -0700 | [diff] [blame] | 120 | } |
| 121 | else |
| 122 | { |
| 123 | NFD_LOG_INFO("command result: unsupported verb: " << verb); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 124 | sendResponse(commandName, 501, "Unsupported"); |
Steve DiBenedetto | 471c060 | 2014-02-18 12:32:00 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | NFD_LOG_INFO("command result: unsupported module: " << module); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 130 | sendResponse(commandName, 501, "Unsupported"); |
Steve DiBenedetto | 471c060 | 2014-02-18 12:32:00 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | } // namespace nfd |
| 135 | |