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