blob: aae1d0ce188c8bf57a001c4a9eacd60edfca529f [file] [log] [blame]
Steve DiBenedetto471c0602014-02-18 12:32:00 -07001/* -*- 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 Afanasyevbd220a02014-02-20 00:29:56 -08008#include "face/local-face.hpp"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07009#include "mgmt/internal-face.hpp"
Steve DiBenedetto471c0602014-02-18 12:32:00 -070010
11namespace nfd {
12
13NFD_LOG_INIT("LocalControlHeaderManager");
14
15const Name LocalControlHeaderManager::COMMAND_PREFIX = "/localhost/nfd/control-header";
16
17const size_t LocalControlHeaderManager::COMMAND_UNSIGNED_NCOMPS =
18 LocalControlHeaderManager::COMMAND_PREFIX.size() +
19 1 + // control-module
20 1; // verb
21
22const size_t LocalControlHeaderManager::COMMAND_SIGNED_NCOMPS =
23 LocalControlHeaderManager::COMMAND_UNSIGNED_NCOMPS +
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070024 4; // (timestamp, nonce, signed info tlv, signature tlv)
Steve DiBenedetto471c0602014-02-18 12:32:00 -070025
26
27LocalControlHeaderManager::LocalControlHeaderManager(function<shared_ptr<Face>(FaceId)> getFace,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070028 shared_ptr<InternalFace> face)
29 : ManagerBase(face, CONTROL_HEADER_PRIVILEGE),
Steve DiBenedetto471c0602014-02-18 12:32:00 -070030 m_getFace(getFace)
31{
32 face->setInterestFilter("/localhost/nfd/control-header",
33 bind(&LocalControlHeaderManager::onLocalControlHeaderRequest, this, _2));
34}
35
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070036
37
Steve DiBenedetto471c0602014-02-18 12:32:00 -070038void
39LocalControlHeaderManager::onLocalControlHeaderRequest(const Interest& request)
40{
Steve DiBenedetto471c0602014-02-18 12:32:00 -070041 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 DiBenedetto2c2b8892014-02-27 11:46:48 -070060 validate(request,
61 bind(&LocalControlHeaderManager::onCommandValidated,
62 this, _1),
63 bind(&ManagerBase::onCommandValidationFailed,
64 this, _1, _2));
65
66
67}
68
69void
70LocalControlHeaderManager::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 Afanasyevbd220a02014-02-20 00:29:56 -080077 shared_ptr<LocalFace> face =
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070078 dynamic_pointer_cast<LocalFace>(m_getFace(command->getIncomingFaceId()));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080079
80 if (!static_cast<bool>(face))
81 {
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070082 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 Afanasyevbd220a02014-02-20 00:29:56 -080084 return;
85 }
Steve DiBenedetto471c0602014-02-18 12:32:00 -070086
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070087 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 DiBenedetto471c0602014-02-18 12:32:00 -070090
91 if (module == MODULE_IN_FACEID)
92 {
93 if (verb == VERB_ENABLE)
94 {
95 face->setLocalControlHeaderFeature(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID, true);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070096 sendResponse(commandName, 200, "Success");
Steve DiBenedetto471c0602014-02-18 12:32:00 -070097 }
98 else if (verb == VERB_DISABLE)
99 {
100 face->setLocalControlHeaderFeature(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID, false);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700101 sendResponse(commandName, 200, "Success");
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700102 }
103 else
104 {
105 NFD_LOG_INFO("command result: unsupported verb: " << verb);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700106 sendResponse(commandName, 501, "Unsupported");
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700107 }
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 DiBenedetto2c2b8892014-02-27 11:46:48 -0700114 sendResponse(commandName, 200, "Success");
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700115 }
116 else if (verb == VERB_DISABLE)
117 {
118 face->setLocalControlHeaderFeature(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID, false);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700119 sendResponse(commandName, 200, "Success");
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700120 }
121 else
122 {
123 NFD_LOG_INFO("command result: unsupported verb: " << verb);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700124 sendResponse(commandName, 501, "Unsupported");
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700125 }
126 }
127 else
128 {
129 NFD_LOG_INFO("command result: unsupported module: " << module);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700130 sendResponse(commandName, 501, "Unsupported");
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700131 }
132}
133
134} // namespace nfd
135