blob: 5717f543493020f40da85fbd2044fb76bdf5a5a7 [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 DiBenedetto471c0602014-02-18 12:32:00 -07009
10namespace nfd {
11
12NFD_LOG_INIT("LocalControlHeaderManager");
13
14const Name LocalControlHeaderManager::COMMAND_PREFIX = "/localhost/nfd/control-header";
15
16const size_t LocalControlHeaderManager::COMMAND_UNSIGNED_NCOMPS =
17 LocalControlHeaderManager::COMMAND_PREFIX.size() +
18 1 + // control-module
19 1; // verb
20
21const size_t LocalControlHeaderManager::COMMAND_SIGNED_NCOMPS =
22 LocalControlHeaderManager::COMMAND_UNSIGNED_NCOMPS +
23 0; // No signed Interest support in mock
24
25
26LocalControlHeaderManager::LocalControlHeaderManager(function<shared_ptr<Face>(FaceId)> getFace,
27 shared_ptr<AppFace> face)
28 : ManagerBase(face),
29 m_getFace(getFace)
30{
31 face->setInterestFilter("/localhost/nfd/control-header",
32 bind(&LocalControlHeaderManager::onLocalControlHeaderRequest, this, _2));
33}
34
35void
36LocalControlHeaderManager::onLocalControlHeaderRequest(const Interest& request)
37{
38 static const Name::Component MODULE_IN_FACEID("in-faceid");
39 static const Name::Component MODULE_NEXTHOP_FACEID("nexthop-faceid");
40 static const Name::Component VERB_ENABLE("enable");
41 static const Name::Component VERB_DISABLE("disable");
42
43 const Name& command = request.getName();
44 const size_t commandNComps = command.size();
45
46 if (COMMAND_UNSIGNED_NCOMPS <= commandNComps &&
47 commandNComps < COMMAND_SIGNED_NCOMPS)
48 {
49 NFD_LOG_INFO("command result: unsigned verb: " << command);
50 sendResponse(command, 401, "Signature required");
51
52 return;
53 }
54 else if (commandNComps < COMMAND_SIGNED_NCOMPS ||
55 !COMMAND_PREFIX.isPrefixOf(command))
56 {
57 NFD_LOG_INFO("command result: malformed");
58 sendResponse(command, 400, "Malformed command");
59 return;
60 }
61
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080062 shared_ptr<LocalFace> face =
63 dynamic_pointer_cast<LocalFace>(m_getFace(request.getIncomingFaceId()));
64
65 if (!static_cast<bool>(face))
66 {
67 NFD_LOG_INFO("command result: request to enable control header on non-local face");
68 sendResponse(command, 400, "Command not supported on the requested face");
69 return;
70 }
Steve DiBenedetto471c0602014-02-18 12:32:00 -070071
72 const Name::Component& module = command.get(COMMAND_PREFIX.size());
73 const Name::Component& verb = command.get(COMMAND_PREFIX.size() + 1);
74
75 if (module == MODULE_IN_FACEID)
76 {
77 if (verb == VERB_ENABLE)
78 {
79 face->setLocalControlHeaderFeature(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID, true);
80 sendResponse(command, 200, "Success");
81 }
82 else if (verb == VERB_DISABLE)
83 {
84 face->setLocalControlHeaderFeature(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID, false);
85 sendResponse(command, 200, "Success");
86 }
87 else
88 {
89 NFD_LOG_INFO("command result: unsupported verb: " << verb);
90 sendResponse(command, 501, "Unsupported");
91 }
92 }
93 else if (module == MODULE_NEXTHOP_FACEID)
94 {
95 if (verb == VERB_ENABLE)
96 {
97 face->setLocalControlHeaderFeature(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID, true);
98 sendResponse(command, 200, "Success");
99 }
100 else if (verb == VERB_DISABLE)
101 {
102 face->setLocalControlHeaderFeature(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID, false);
103 sendResponse(command, 200, "Success");
104 }
105 else
106 {
107 NFD_LOG_INFO("command result: unsupported verb: " << verb);
108 sendResponse(command, 501, "Unsupported");
109 }
110 }
111 else
112 {
113 NFD_LOG_INFO("command result: unsupported module: " << module);
114 sendResponse(command, 501, "Unsupported");
115 }
116}
117
118} // namespace nfd
119