Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_FACE_LOCAL_FACE_HPP |
| 27 | #define NFD_DAEMON_FACE_LOCAL_FACE_HPP |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 28 | |
| 29 | #include "face.hpp" |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 30 | #include <ndn-cxx/management/nfd-control-parameters.hpp> |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 31 | |
| 32 | namespace nfd { |
| 33 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 34 | using ndn::nfd::LocalControlFeature; |
| 35 | using ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID; |
| 36 | using ndn::nfd::LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 37 | |
| 38 | /** \brief represents a face |
| 39 | */ |
| 40 | class LocalFace : public Face |
| 41 | { |
| 42 | public: |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 43 | LocalFace(const FaceUri& remoteUri, const FaceUri& localUri); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 44 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 45 | /** \brief get whether any LocalControlHeader feature is enabled |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 46 | * |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 47 | * \returns true if any feature is enabled. |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 48 | */ |
| 49 | bool |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 50 | isLocalControlHeaderEnabled() const; |
| 51 | |
| 52 | /** \brief get whether a specific LocalControlHeader feature is enabled |
| 53 | * |
| 54 | * \param feature The feature. |
| 55 | * \returns true if the specified feature is enabled. |
| 56 | */ |
| 57 | bool |
| 58 | isLocalControlHeaderEnabled(LocalControlFeature feature) const; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 59 | |
| 60 | /** \brief enable or disable a LocalControlHeader feature |
| 61 | * |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 62 | * \param feature The feature. Cannot be LOCAL_CONTROL_FEATURE_ANY |
| 63 | * or LOCAL_CONTROL_FEATURE_MAX |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 64 | * \param enabled true/false enable/disable the feature |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 65 | */ |
| 66 | void |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 67 | setLocalControlHeaderFeature(LocalControlFeature feature, bool enabled = true); |
| 68 | |
| 69 | public: |
| 70 | |
| 71 | static const size_t LOCAL_CONTROL_FEATURE_MAX = 3; /// upper bound of LocalControlFeature enum |
| 72 | static const size_t LOCAL_CONTROL_FEATURE_ANY = 0; /// any feature |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 73 | |
| 74 | protected: |
| 75 | // statically overridden from Face |
| 76 | |
| 77 | /** \brief Decode block into Interest/Data, considering potential LocalControlHeader |
| 78 | * |
| 79 | * If LocalControlHeader is present, the encoded data is filtered out, based |
| 80 | * on enabled features on the face. |
| 81 | */ |
| 82 | bool |
| 83 | decodeAndDispatchInput(const Block& element); |
| 84 | |
| 85 | // LocalFace-specific methods |
| 86 | |
| 87 | /** \brief Check if LocalControlHeader needs to be included, taking into account |
| 88 | * both set parameters in supplied LocalControlHeader and features |
| 89 | * enabled on the local face. |
| 90 | */ |
| 91 | bool |
| 92 | isEmptyFilteredLocalControlHeader(const ndn::nfd::LocalControlHeader& header) const; |
| 93 | |
| 94 | /** \brief Create LocalControlHeader, considering enabled features |
| 95 | */ |
| 96 | template<class Packet> |
| 97 | Block |
| 98 | filterAndEncodeLocalControlHeader(const Packet& packet); |
| 99 | |
| 100 | private: |
| 101 | std::vector<bool> m_localControlHeaderFeatures; |
| 102 | }; |
| 103 | |
| 104 | inline |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 105 | LocalFace::LocalFace(const FaceUri& remoteUri, const FaceUri& localUri) |
| 106 | : Face(remoteUri, localUri, true) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 107 | , m_localControlHeaderFeatures(LocalFace::LOCAL_CONTROL_FEATURE_MAX) |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 108 | { |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | inline bool |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 112 | LocalFace::isLocalControlHeaderEnabled() const |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 113 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 114 | return m_localControlHeaderFeatures[LOCAL_CONTROL_FEATURE_ANY]; |
| 115 | } |
| 116 | |
| 117 | inline bool |
| 118 | LocalFace::isLocalControlHeaderEnabled(LocalControlFeature feature) const |
| 119 | { |
Alexander Afanasyev | 85b6b01 | 2014-04-21 18:12:57 -0700 | [diff] [blame] | 120 | BOOST_ASSERT(0 < feature && |
| 121 | static_cast<size_t>(feature) < m_localControlHeaderFeatures.size()); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 122 | return m_localControlHeaderFeatures[feature]; |
| 123 | } |
| 124 | |
| 125 | inline void |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 126 | LocalFace::setLocalControlHeaderFeature(LocalControlFeature feature, bool enabled/* = true*/) |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 127 | { |
Alexander Afanasyev | 85b6b01 | 2014-04-21 18:12:57 -0700 | [diff] [blame] | 128 | BOOST_ASSERT(0 < feature && |
| 129 | static_cast<size_t>(feature) < m_localControlHeaderFeatures.size()); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 130 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 131 | m_localControlHeaderFeatures[feature] = enabled; |
| 132 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 133 | m_localControlHeaderFeatures[LOCAL_CONTROL_FEATURE_ANY] = |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 134 | std::find(m_localControlHeaderFeatures.begin() + 1, |
| 135 | m_localControlHeaderFeatures.end(), true) < |
| 136 | m_localControlHeaderFeatures.end(); |
| 137 | // 'find(..) < .end()' instead of 'find(..) != .end()' due to LLVM Bug 16816 |
| 138 | } |
| 139 | |
| 140 | inline bool |
| 141 | LocalFace::decodeAndDispatchInput(const Block& element) |
| 142 | { |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 143 | try { |
| 144 | const Block& payload = ndn::nfd::LocalControlHeader::getPayload(element); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 145 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 146 | // If received LocalControlHeader, but it is not enabled on the face |
| 147 | if ((&payload != &element) && !this->isLocalControlHeaderEnabled()) |
| 148 | return false; |
| 149 | |
| 150 | if (payload.type() == tlv::Interest) |
| 151 | { |
| 152 | shared_ptr<Interest> i = make_shared<Interest>(); |
| 153 | i->wireDecode(payload); |
| 154 | if (&payload != &element) |
| 155 | { |
Alexander Afanasyev | a4ff44c | 2015-02-13 14:11:37 -0800 | [diff] [blame] | 156 | uint8_t mask = 0; |
| 157 | if (this->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID)) { |
| 158 | mask |= ndn::nfd::LocalControlHeader::ENCODE_NEXT_HOP; |
| 159 | } |
| 160 | i->getLocalControlHeader().wireDecode(element, mask); |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 163 | this->emitSignal(onReceiveInterest, *i); |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 164 | } |
| 165 | else if (payload.type() == tlv::Data) |
| 166 | { |
| 167 | shared_ptr<Data> d = make_shared<Data>(); |
| 168 | d->wireDecode(payload); |
| 169 | |
| 170 | /// \todo Uncomment and correct the following when we have more |
| 171 | /// options in LocalControlHeader that apply for incoming |
| 172 | /// Data packets (if ever) |
| 173 | // if (&payload != &element) |
| 174 | // { |
| 175 | // |
| 176 | // d->getLocalControlHeader().wireDecode(element, |
| 177 | // false, |
| 178 | // false); |
| 179 | // } |
| 180 | |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 181 | this->emitSignal(onReceiveData, *d); |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 182 | } |
| 183 | else |
| 184 | return false; |
| 185 | |
| 186 | return true; |
| 187 | } |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 188 | catch (const tlv::Error&) { |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 189 | return false; |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 190 | } |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | inline bool |
| 194 | LocalFace::isEmptyFilteredLocalControlHeader(const ndn::nfd::LocalControlHeader& header) const |
| 195 | { |
| 196 | if (!this->isLocalControlHeaderEnabled()) |
| 197 | return true; |
| 198 | |
Alexander Afanasyev | a4ff44c | 2015-02-13 14:11:37 -0800 | [diff] [blame] | 199 | uint8_t mask = 0; |
| 200 | if (this->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID)) { |
| 201 | mask |= ndn::nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID; |
| 202 | } |
| 203 | return header.empty(mask); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | template<class Packet> |
| 207 | inline Block |
| 208 | LocalFace::filterAndEncodeLocalControlHeader(const Packet& packet) |
| 209 | { |
Alexander Afanasyev | a4ff44c | 2015-02-13 14:11:37 -0800 | [diff] [blame] | 210 | uint8_t mask = 0; |
| 211 | if (this->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID)) { |
| 212 | mask |= ndn::nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID; |
| 213 | } |
| 214 | return packet.getLocalControlHeader().wireEncode(packet, mask); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | } // namespace nfd |
| 218 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 219 | #endif // NFD_DAEMON_FACE_LOCAL_FACE_HPP |