Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014, 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/>. |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 24 | */ |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 25 | |
| 26 | #include "face-manager.hpp" |
| 27 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 28 | #include "core/logger.hpp" |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 29 | #include "core/face-uri.hpp" |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 30 | #include "core/network-interface.hpp" |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 31 | #include "fw/face-table.hpp" |
Alexander Afanasyev | e515f0a | 2014-06-30 15:28:10 -0700 | [diff] [blame] | 32 | #include "face/face-flags.hpp" |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 33 | #include "face/tcp-factory.hpp" |
| 34 | #include "face/udp-factory.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 35 | #include "core/config-file.hpp" |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 36 | |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 37 | #ifdef HAVE_UNIX_SOCKETS |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 38 | #include "face/unix-stream-factory.hpp" |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 39 | #endif // HAVE_UNIX_SOCKETS |
| 40 | |
Alexander Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 41 | #ifdef HAVE_LIBPCAP |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 42 | #include "face/ethernet-factory.hpp" |
Alexander Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 43 | #endif // HAVE_LIBPCAP |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 44 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 45 | #ifdef HAVE_WEBSOCKET |
| 46 | #include "face/websocket-factory.hpp" |
| 47 | #endif // HAVE_WEBSOCKET |
| 48 | |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 49 | #include <ndn-cxx/management/nfd-face-event-notification.hpp> |
Davide Pesavento | 52a18f9 | 2014-04-10 00:55:01 +0200 | [diff] [blame] | 50 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 51 | namespace nfd { |
| 52 | |
| 53 | NFD_LOG_INIT("FaceManager"); |
| 54 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 55 | const Name FaceManager::COMMAND_PREFIX("/localhost/nfd/faces"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 56 | |
| 57 | const size_t FaceManager::COMMAND_UNSIGNED_NCOMPS = |
| 58 | FaceManager::COMMAND_PREFIX.size() + |
| 59 | 1 + // verb |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 60 | 1; // verb parameters |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 61 | |
| 62 | const size_t FaceManager::COMMAND_SIGNED_NCOMPS = |
| 63 | FaceManager::COMMAND_UNSIGNED_NCOMPS + |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 64 | 4; // (timestamp, nonce, signed info tlv, signature tlv) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 65 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 66 | const FaceManager::SignedVerbAndProcessor FaceManager::SIGNED_COMMAND_VERBS[] = |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 67 | { |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 68 | SignedVerbAndProcessor( |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 69 | Name::Component("create"), |
| 70 | &FaceManager::createFace |
| 71 | ), |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 72 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 73 | SignedVerbAndProcessor( |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 74 | Name::Component("destroy"), |
| 75 | &FaceManager::destroyFace |
| 76 | ), |
| 77 | |
| 78 | SignedVerbAndProcessor( |
| 79 | Name::Component("enable-local-control"), |
| 80 | &FaceManager::enableLocalControl |
| 81 | ), |
| 82 | |
| 83 | SignedVerbAndProcessor( |
| 84 | Name::Component("disable-local-control"), |
| 85 | &FaceManager::disableLocalControl |
| 86 | ), |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 89 | const FaceManager::UnsignedVerbAndProcessor FaceManager::UNSIGNED_COMMAND_VERBS[] = |
| 90 | { |
| 91 | UnsignedVerbAndProcessor( |
| 92 | Name::Component("list"), |
| 93 | &FaceManager::listFaces |
| 94 | ), |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 95 | |
| 96 | UnsignedVerbAndProcessor( |
| 97 | Name::Component("events"), |
| 98 | &FaceManager::ignoreUnsignedVerb |
| 99 | ), |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 100 | |
| 101 | UnsignedVerbAndProcessor( |
| 102 | Name::Component("channels"), |
| 103 | &FaceManager::listChannels |
| 104 | ), |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 105 | }; |
| 106 | |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 107 | const Name FaceManager::FACES_LIST_DATASET_PREFIX("/localhost/nfd/faces/list"); |
| 108 | const size_t FaceManager::FACES_LIST_DATASET_NCOMPS = FACES_LIST_DATASET_PREFIX.size(); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 109 | |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 110 | const Name FaceManager::FACE_EVENTS_PREFIX("/localhost/nfd/faces/events"); |
| 111 | |
| 112 | const Name FaceManager::CHANNELS_LIST_DATASET_PREFIX("/localhost/nfd/faces/channels"); |
| 113 | const size_t FaceManager::CHANNELS_LIST_DATASET_NCOMPS = CHANNELS_LIST_DATASET_PREFIX.size(); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 114 | |
| 115 | FaceManager::FaceManager(FaceTable& faceTable, |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame^] | 116 | shared_ptr<InternalFace> face, |
| 117 | ndn::KeyChain& keyChain) |
| 118 | : ManagerBase(face, FACE_MANAGER_PRIVILEGE, keyChain) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 119 | , m_faceTable(faceTable) |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame^] | 120 | , m_faceStatusPublisher(m_faceTable, *m_face, FACES_LIST_DATASET_PREFIX, keyChain) |
| 121 | , m_channelStatusPublisher(m_factories, *m_face, CHANNELS_LIST_DATASET_PREFIX, keyChain) |
| 122 | , m_notificationStream(m_face, FACE_EVENTS_PREFIX, keyChain) |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 123 | , m_signedVerbDispatch(SIGNED_COMMAND_VERBS, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 124 | SIGNED_COMMAND_VERBS + |
| 125 | (sizeof(SIGNED_COMMAND_VERBS) / sizeof(SignedVerbAndProcessor))) |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 126 | , m_unsignedVerbDispatch(UNSIGNED_COMMAND_VERBS, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 127 | UNSIGNED_COMMAND_VERBS + |
| 128 | (sizeof(UNSIGNED_COMMAND_VERBS) / sizeof(UnsignedVerbAndProcessor))) |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 129 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 130 | { |
| 131 | face->setInterestFilter("/localhost/nfd/faces", |
| 132 | bind(&FaceManager::onFaceRequest, this, _2)); |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 133 | |
| 134 | m_faceTable.onAdd += bind(&FaceManager::onAddFace, this, _1); |
| 135 | m_faceTable.onRemove += bind(&FaceManager::onRemoveFace, this, _1); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 138 | FaceManager::~FaceManager() |
| 139 | { |
| 140 | |
| 141 | } |
| 142 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 143 | void |
| 144 | FaceManager::setConfigFile(ConfigFile& configFile) |
| 145 | { |
| 146 | configFile.addSectionHandler("face_system", |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 147 | bind(&FaceManager::onConfig, this, _1, _2, _3)); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
| 151 | void |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 152 | FaceManager::onConfig(const ConfigSection& configSection, |
| 153 | bool isDryRun, |
| 154 | const std::string& filename) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 155 | { |
| 156 | bool hasSeenUnix = false; |
| 157 | bool hasSeenTcp = false; |
| 158 | bool hasSeenUdp = false; |
| 159 | bool hasSeenEther = false; |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 160 | bool hasSeenWebSocket = false; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 161 | |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 162 | const std::list<shared_ptr<NetworkInterfaceInfo> > nicList(listNetworkInterfaces()); |
| 163 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 164 | for (ConfigSection::const_iterator item = configSection.begin(); |
| 165 | item != configSection.end(); |
| 166 | ++item) |
| 167 | { |
| 168 | if (item->first == "unix") |
| 169 | { |
| 170 | if (hasSeenUnix) |
| 171 | throw Error("Duplicate \"unix\" section"); |
| 172 | hasSeenUnix = true; |
| 173 | |
| 174 | processSectionUnix(item->second, isDryRun); |
| 175 | } |
| 176 | else if (item->first == "tcp") |
| 177 | { |
| 178 | if (hasSeenTcp) |
| 179 | throw Error("Duplicate \"tcp\" section"); |
| 180 | hasSeenTcp = true; |
| 181 | |
| 182 | processSectionTcp(item->second, isDryRun); |
| 183 | } |
| 184 | else if (item->first == "udp") |
| 185 | { |
| 186 | if (hasSeenUdp) |
| 187 | throw Error("Duplicate \"udp\" section"); |
| 188 | hasSeenUdp = true; |
| 189 | |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 190 | processSectionUdp(item->second, isDryRun, nicList); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 191 | } |
| 192 | else if (item->first == "ether") |
| 193 | { |
| 194 | if (hasSeenEther) |
| 195 | throw Error("Duplicate \"ether\" section"); |
| 196 | hasSeenEther = true; |
| 197 | |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 198 | processSectionEther(item->second, isDryRun, nicList); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 199 | } |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 200 | else if (item->first == "websocket") |
| 201 | { |
| 202 | if (hasSeenWebSocket) |
| 203 | throw Error("Duplicate \"websocket\" section"); |
| 204 | hasSeenWebSocket = true; |
| 205 | |
| 206 | processSectionWebSocket(item->second, isDryRun); |
| 207 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 208 | else |
| 209 | { |
| 210 | throw Error("Unrecognized option \"" + item->first + "\""); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | void |
| 216 | FaceManager::processSectionUnix(const ConfigSection& configSection, bool isDryRun) |
| 217 | { |
| 218 | // ; the unix section contains settings of UNIX stream faces and channels |
| 219 | // unix |
| 220 | // { |
| 221 | // listen yes ; set to 'no' to disable UNIX stream listener, default 'yes' |
| 222 | // path /var/run/nfd.sock ; UNIX stream listener path |
| 223 | // } |
| 224 | |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 225 | #if defined(HAVE_UNIX_SOCKETS) |
| 226 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 227 | bool needToListen = true; |
| 228 | std::string path = "/var/run/nfd.sock"; |
| 229 | |
| 230 | for (ConfigSection::const_iterator i = configSection.begin(); |
| 231 | i != configSection.end(); |
| 232 | ++i) |
| 233 | { |
| 234 | if (i->first == "path") |
| 235 | { |
| 236 | path = i->second.get_value<std::string>(); |
| 237 | } |
| 238 | else if (i->first == "listen") |
| 239 | { |
| 240 | needToListen = parseYesNo(i, i->first, "unix"); |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | throw ConfigFile::Error("Unrecognized option \"" + i->first + "\" in \"unix\" section"); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if (!isDryRun) |
| 249 | { |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 250 | if (m_factories.count("unix") > 0) |
| 251 | { |
| 252 | return; |
| 253 | // shared_ptr<UnixStreamFactory> factory |
| 254 | // = static_pointer_cast<UnixStreamFactory>(m_factories["unix"]); |
| 255 | // shared_ptr<UnixStreamChannel> unixChannel = factory->findChannel(path); |
| 256 | |
| 257 | // if (static_cast<bool>(unixChannel)) |
| 258 | // { |
| 259 | // return; |
| 260 | // } |
| 261 | } |
| 262 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 263 | shared_ptr<UnixStreamFactory> factory = make_shared<UnixStreamFactory>(); |
| 264 | shared_ptr<UnixStreamChannel> unixChannel = factory->createChannel(path); |
| 265 | |
| 266 | if (needToListen) |
| 267 | { |
| 268 | // Should acceptFailed callback be used somehow? |
| 269 | unixChannel->listen(bind(&FaceTable::add, &m_faceTable, _1), |
| 270 | UnixStreamChannel::ConnectFailedCallback()); |
| 271 | } |
| 272 | |
| 273 | m_factories.insert(std::make_pair("unix", factory)); |
| 274 | } |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 275 | #else |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 276 | throw ConfigFile::Error("NFD was compiled without UNIX sockets support, " |
| 277 | "cannot process \"unix\" section"); |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 278 | #endif // HAVE_UNIX_SOCKETS |
| 279 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void |
| 283 | FaceManager::processSectionTcp(const ConfigSection& configSection, bool isDryRun) |
| 284 | { |
| 285 | // ; the tcp section contains settings of TCP faces and channels |
| 286 | // tcp |
| 287 | // { |
| 288 | // listen yes ; set to 'no' to disable TCP listener, default 'yes' |
| 289 | // port 6363 ; TCP listener port number |
| 290 | // } |
| 291 | |
| 292 | std::string port = "6363"; |
| 293 | bool needToListen = true; |
Steve DiBenedetto | 9515287 | 2014-04-11 12:40:59 -0600 | [diff] [blame] | 294 | bool enableV4 = true; |
| 295 | bool enableV6 = true; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 296 | |
| 297 | for (ConfigSection::const_iterator i = configSection.begin(); |
| 298 | i != configSection.end(); |
| 299 | ++i) |
| 300 | { |
| 301 | if (i->first == "port") |
| 302 | { |
| 303 | port = i->second.get_value<std::string>(); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 304 | try |
| 305 | { |
| 306 | uint16_t portNo = boost::lexical_cast<uint16_t>(port); |
| 307 | NFD_LOG_TRACE("TCP port set to " << portNo); |
| 308 | } |
| 309 | catch (const std::bad_cast& error) |
| 310 | { |
| 311 | throw ConfigFile::Error("Invalid value for option " + |
Steve DiBenedetto | 9515287 | 2014-04-11 12:40:59 -0600 | [diff] [blame] | 312 | i->first + "\" in \"tcp\" section"); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 313 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 314 | } |
| 315 | else if (i->first == "listen") |
| 316 | { |
| 317 | needToListen = parseYesNo(i, i->first, "tcp"); |
| 318 | } |
Steve DiBenedetto | 9515287 | 2014-04-11 12:40:59 -0600 | [diff] [blame] | 319 | else if (i->first == "enable_v4") |
| 320 | { |
| 321 | enableV4 = parseYesNo(i, i->first, "tcp"); |
| 322 | } |
| 323 | else if (i->first == "enable_v6") |
| 324 | { |
| 325 | enableV6 = parseYesNo(i, i->first, "tcp"); |
| 326 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 327 | else |
| 328 | { |
| 329 | throw ConfigFile::Error("Unrecognized option \"" + i->first + "\" in \"tcp\" section"); |
| 330 | } |
| 331 | } |
| 332 | |
Steve DiBenedetto | 9515287 | 2014-04-11 12:40:59 -0600 | [diff] [blame] | 333 | if (!enableV4 && !enableV6) |
| 334 | { |
| 335 | throw ConfigFile::Error("IPv4 and IPv6 channels have been disabled." |
| 336 | " Remove \"tcp\" section to disable TCP channels or" |
| 337 | " re-enable at least one channel type."); |
| 338 | } |
| 339 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 340 | if (!isDryRun) |
| 341 | { |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 342 | if (m_factories.count("tcp") > 0) |
| 343 | { |
| 344 | return; |
| 345 | } |
| 346 | |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 347 | shared_ptr<TcpFactory> factory = ndn::make_shared<TcpFactory>(port); |
Steve DiBenedetto | 9515287 | 2014-04-11 12:40:59 -0600 | [diff] [blame] | 348 | m_factories.insert(std::make_pair("tcp", factory)); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 349 | |
Steve DiBenedetto | 9515287 | 2014-04-11 12:40:59 -0600 | [diff] [blame] | 350 | if (enableV4) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 351 | { |
Steve DiBenedetto | 9515287 | 2014-04-11 12:40:59 -0600 | [diff] [blame] | 352 | shared_ptr<TcpChannel> ipv4Channel = factory->createChannel("0.0.0.0", port); |
| 353 | if (needToListen) |
| 354 | { |
| 355 | // Should acceptFailed callback be used somehow? |
| 356 | ipv4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), |
| 357 | TcpChannel::ConnectFailedCallback()); |
| 358 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 359 | |
Steve DiBenedetto | 9515287 | 2014-04-11 12:40:59 -0600 | [diff] [blame] | 360 | m_factories.insert(std::make_pair("tcp4", factory)); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Steve DiBenedetto | 9515287 | 2014-04-11 12:40:59 -0600 | [diff] [blame] | 363 | if (enableV6) |
| 364 | { |
| 365 | shared_ptr<TcpChannel> ipv6Channel = factory->createChannel("::", port); |
| 366 | if (needToListen) |
| 367 | { |
| 368 | // Should acceptFailed callback be used somehow? |
| 369 | ipv6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), |
| 370 | TcpChannel::ConnectFailedCallback()); |
| 371 | } |
| 372 | |
| 373 | m_factories.insert(std::make_pair("tcp6", factory)); |
| 374 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
| 378 | void |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 379 | FaceManager::processSectionUdp(const ConfigSection& configSection, |
| 380 | bool isDryRun, |
| 381 | const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 382 | { |
| 383 | // ; the udp section contains settings of UDP faces and channels |
| 384 | // udp |
| 385 | // { |
| 386 | // port 6363 ; UDP unicast port number |
| 387 | // idle_timeout 30 ; idle time (seconds) before closing a UDP unicast face |
| 388 | // keep_alive_interval 25; interval (seconds) between keep-alive refreshes |
| 389 | |
| 390 | // ; NFD creates one UDP multicast face per NIC |
| 391 | // mcast yes ; set to 'no' to disable UDP multicast, default 'yes' |
| 392 | // mcast_port 56363 ; UDP multicast port number |
| 393 | // mcast_group 224.0.23.170 ; UDP multicast group (IPv4 only) |
| 394 | // } |
| 395 | |
| 396 | std::string port = "6363"; |
Steve DiBenedetto | 9515287 | 2014-04-11 12:40:59 -0600 | [diff] [blame] | 397 | bool enableV4 = true; |
| 398 | bool enableV6 = true; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 399 | size_t timeout = 30; |
| 400 | size_t keepAliveInterval = 25; |
| 401 | bool useMcast = true; |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 402 | std::string mcastGroup = "224.0.23.170"; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 403 | std::string mcastPort = "56363"; |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 404 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 405 | |
| 406 | for (ConfigSection::const_iterator i = configSection.begin(); |
| 407 | i != configSection.end(); |
| 408 | ++i) |
| 409 | { |
| 410 | if (i->first == "port") |
| 411 | { |
| 412 | port = i->second.get_value<std::string>(); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 413 | try |
| 414 | { |
| 415 | uint16_t portNo = boost::lexical_cast<uint16_t>(port); |
| 416 | NFD_LOG_TRACE("UDP port set to " << portNo); |
| 417 | } |
| 418 | catch (const std::bad_cast& error) |
| 419 | { |
| 420 | throw ConfigFile::Error("Invalid value for option " + |
| 421 | i->first + "\" in \"udp\" section"); |
| 422 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 423 | } |
Steve DiBenedetto | 9515287 | 2014-04-11 12:40:59 -0600 | [diff] [blame] | 424 | else if (i->first == "enable_v4") |
| 425 | { |
| 426 | enableV4 = parseYesNo(i, i->first, "udp"); |
| 427 | } |
| 428 | else if (i->first == "enable_v6") |
| 429 | { |
| 430 | enableV6 = parseYesNo(i, i->first, "udp"); |
| 431 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 432 | else if (i->first == "idle_timeout") |
| 433 | { |
| 434 | try |
| 435 | { |
| 436 | timeout = i->second.get_value<size_t>(); |
| 437 | } |
| 438 | catch (const std::exception& e) |
| 439 | { |
| 440 | throw ConfigFile::Error("Invalid value for option \"" + |
| 441 | i->first + "\" in \"udp\" section"); |
| 442 | } |
| 443 | } |
| 444 | else if (i->first == "keep_alive_interval") |
| 445 | { |
| 446 | try |
| 447 | { |
| 448 | keepAliveInterval = i->second.get_value<size_t>(); |
Alexander Afanasyev | efea8fe | 2014-03-23 00:00:35 -0700 | [diff] [blame] | 449 | |
| 450 | /// \todo Make use of keepAliveInterval |
| 451 | (void)(keepAliveInterval); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 452 | } |
| 453 | catch (const std::exception& e) |
| 454 | { |
| 455 | throw ConfigFile::Error("Invalid value for option \"" + |
| 456 | i->first + "\" in \"udp\" section"); |
| 457 | } |
| 458 | } |
| 459 | else if (i->first == "mcast") |
| 460 | { |
| 461 | useMcast = parseYesNo(i, i->first, "udp"); |
| 462 | } |
| 463 | else if (i->first == "mcast_port") |
| 464 | { |
| 465 | mcastPort = i->second.get_value<std::string>(); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 466 | try |
| 467 | { |
| 468 | uint16_t portNo = boost::lexical_cast<uint16_t>(mcastPort); |
| 469 | NFD_LOG_TRACE("UDP multicast port set to " << portNo); |
| 470 | } |
| 471 | catch (const std::bad_cast& error) |
| 472 | { |
| 473 | throw ConfigFile::Error("Invalid value for option " + |
| 474 | i->first + "\" in \"udp\" section"); |
| 475 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 476 | } |
| 477 | else if (i->first == "mcast_group") |
| 478 | { |
| 479 | using namespace boost::asio::ip; |
| 480 | mcastGroup = i->second.get_value<std::string>(); |
| 481 | try |
| 482 | { |
| 483 | address mcastGroupTest = address::from_string(mcastGroup); |
| 484 | if (!mcastGroupTest.is_v4()) |
| 485 | { |
| 486 | throw ConfigFile::Error("Invalid value for option \"" + |
| 487 | i->first + "\" in \"udp\" section"); |
| 488 | } |
| 489 | } |
| 490 | catch(const std::runtime_error& e) |
| 491 | { |
| 492 | throw ConfigFile::Error("Invalid value for option \"" + |
| 493 | i->first + "\" in \"udp\" section"); |
| 494 | } |
| 495 | } |
| 496 | else |
| 497 | { |
| 498 | throw ConfigFile::Error("Unrecognized option \"" + i->first + "\" in \"udp\" section"); |
| 499 | } |
| 500 | } |
| 501 | |
Steve DiBenedetto | 9515287 | 2014-04-11 12:40:59 -0600 | [diff] [blame] | 502 | if (!enableV4 && !enableV6) |
| 503 | { |
| 504 | throw ConfigFile::Error("IPv4 and IPv6 channels have been disabled." |
| 505 | " Remove \"udp\" section to disable UDP channels or" |
| 506 | " re-enable at least one channel type."); |
| 507 | } |
| 508 | else if (useMcast && !enableV4) |
| 509 | { |
| 510 | throw ConfigFile::Error("IPv4 multicast requested, but IPv4 channels" |
| 511 | " have been disabled (conflicting configuration options set)"); |
| 512 | } |
| 513 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 514 | /// \todo what is keep alive interval used for? |
| 515 | |
| 516 | if (!isDryRun) |
| 517 | { |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 518 | shared_ptr<UdpFactory> factory; |
| 519 | bool isReload = false; |
| 520 | if (m_factories.count("udp") > 0) { |
| 521 | isReload = true; |
| 522 | factory = static_pointer_cast<UdpFactory>(m_factories["udp"]); |
| 523 | } |
| 524 | else { |
| 525 | factory = ndn::make_shared<UdpFactory>(port); |
| 526 | m_factories.insert(std::make_pair("udp", factory)); |
| 527 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 528 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 529 | if (!isReload && enableV4) |
Steve DiBenedetto | 9515287 | 2014-04-11 12:40:59 -0600 | [diff] [blame] | 530 | { |
| 531 | shared_ptr<UdpChannel> v4Channel = |
| 532 | factory->createChannel("0.0.0.0", port, time::seconds(timeout)); |
| 533 | |
| 534 | v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), |
| 535 | UdpChannel::ConnectFailedCallback()); |
| 536 | |
| 537 | m_factories.insert(std::make_pair("udp4", factory)); |
| 538 | } |
| 539 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 540 | if (!isReload && enableV6) |
Steve DiBenedetto | 9515287 | 2014-04-11 12:40:59 -0600 | [diff] [blame] | 541 | { |
| 542 | shared_ptr<UdpChannel> v6Channel = |
| 543 | factory->createChannel("::", port, time::seconds(timeout)); |
| 544 | |
| 545 | v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), |
| 546 | UdpChannel::ConnectFailedCallback()); |
| 547 | m_factories.insert(std::make_pair("udp6", factory)); |
| 548 | } |
| 549 | |
| 550 | if (useMcast && enableV4) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 551 | { |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 552 | std::list<shared_ptr<NetworkInterfaceInfo> > ipv4MulticastInterfaces; |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 553 | for (std::list<shared_ptr<NetworkInterfaceInfo> >::const_iterator i = nicList.begin(); |
| 554 | i != nicList.end(); |
| 555 | ++i) |
| 556 | { |
| 557 | const shared_ptr<NetworkInterfaceInfo>& nic = *i; |
| 558 | if (nic->isUp() && nic->isMulticastCapable() && !nic->ipv4Addresses.empty()) |
| 559 | { |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 560 | ipv4MulticastInterfaces.push_back(nic); |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 561 | } |
| 562 | } |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 563 | |
| 564 | bool isNicNameNecessary = false; |
| 565 | |
| 566 | #if defined(__linux__) |
| 567 | if (ipv4MulticastInterfaces.size() > 1) |
| 568 | { |
| 569 | //On Linux, if we have more than one MulticastUdpFace we need to specify |
| 570 | //the name of the interface |
| 571 | isNicNameNecessary = true; |
| 572 | } |
| 573 | #endif |
| 574 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 575 | std::list<shared_ptr<MulticastUdpFace> > multicastFacesToRemove; |
| 576 | for (UdpFactory::MulticastFaceMap::const_iterator i = |
| 577 | factory->getMulticastFaces().begin(); |
| 578 | i != factory->getMulticastFaces().end(); |
| 579 | ++i) |
| 580 | { |
| 581 | multicastFacesToRemove.push_back(i->second); |
| 582 | } |
| 583 | |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 584 | for (std::list<shared_ptr<NetworkInterfaceInfo> >::const_iterator i = |
| 585 | ipv4MulticastInterfaces.begin(); |
| 586 | i != ipv4MulticastInterfaces.end(); |
| 587 | ++i) |
| 588 | { |
| 589 | const shared_ptr<NetworkInterfaceInfo>& nic = *i; |
| 590 | shared_ptr<MulticastUdpFace> newFace; |
| 591 | newFace = factory->createMulticastFace(nic->ipv4Addresses[0].to_string(), |
| 592 | mcastGroup, |
| 593 | mcastPort, |
| 594 | isNicNameNecessary ? nic->name : ""); |
| 595 | |
| 596 | addCreatedFaceToForwarder(newFace); |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 597 | multicastFacesToRemove.remove(newFace); |
| 598 | } |
| 599 | |
| 600 | for (std::list<shared_ptr<MulticastUdpFace> >::iterator i = |
| 601 | multicastFacesToRemove.begin(); |
| 602 | i != multicastFacesToRemove.end(); |
| 603 | ++i) |
| 604 | { |
| 605 | (*i)->close(); |
| 606 | } |
| 607 | } |
| 608 | else |
| 609 | { |
| 610 | std::list<shared_ptr<MulticastUdpFace> > multicastFacesToRemove; |
| 611 | for (UdpFactory::MulticastFaceMap::const_iterator i = |
| 612 | factory->getMulticastFaces().begin(); |
| 613 | i != factory->getMulticastFaces().end(); |
| 614 | ++i) |
| 615 | { |
| 616 | multicastFacesToRemove.push_back(i->second); |
| 617 | } |
| 618 | |
| 619 | for (std::list<shared_ptr<MulticastUdpFace> >::iterator i = |
| 620 | multicastFacesToRemove.begin(); |
| 621 | i != multicastFacesToRemove.end(); |
| 622 | ++i) |
| 623 | { |
| 624 | (*i)->close(); |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 625 | } |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 626 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
| 630 | void |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 631 | FaceManager::processSectionEther(const ConfigSection& configSection, |
| 632 | bool isDryRun, |
| 633 | const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 634 | { |
| 635 | // ; the ether section contains settings of Ethernet faces and channels |
| 636 | // ether |
| 637 | // { |
| 638 | // ; NFD creates one Ethernet multicast face per NIC |
| 639 | // mcast yes ; set to 'no' to disable Ethernet multicast, default 'yes' |
| 640 | // mcast_group 01:00:5E:00:17:AA ; Ethernet multicast group |
| 641 | // } |
| 642 | |
Alexander Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 643 | #if defined(HAVE_LIBPCAP) |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 644 | |
| 645 | using ethernet::Address; |
| 646 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 647 | bool useMcast = true; |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 648 | Address mcastGroup(ethernet::getDefaultMulticastAddress()); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 649 | |
| 650 | for (ConfigSection::const_iterator i = configSection.begin(); |
| 651 | i != configSection.end(); |
| 652 | ++i) |
| 653 | { |
| 654 | if (i->first == "mcast") |
| 655 | { |
| 656 | useMcast = parseYesNo(i, i->first, "ether"); |
| 657 | } |
| 658 | |
| 659 | else if (i->first == "mcast_group") |
| 660 | { |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 661 | mcastGroup = Address::fromString(i->second.get_value<std::string>()); |
| 662 | if (mcastGroup.isNull()) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 663 | { |
| 664 | throw ConfigFile::Error("Invalid value for option \"" + |
| 665 | i->first + "\" in \"ether\" section"); |
| 666 | } |
| 667 | } |
| 668 | else |
| 669 | { |
| 670 | throw ConfigFile::Error("Unrecognized option \"" + i->first + "\" in \"ether\" section"); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | if (!isDryRun) |
| 675 | { |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 676 | shared_ptr<EthernetFactory> factory; |
| 677 | if (m_factories.count("ether") > 0) { |
| 678 | factory = static_pointer_cast<EthernetFactory>(m_factories["ether"]); |
| 679 | } |
| 680 | else { |
| 681 | factory = ndn::make_shared<EthernetFactory>(); |
| 682 | m_factories.insert(std::make_pair("ether", factory)); |
| 683 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 684 | |
| 685 | if (useMcast) |
| 686 | { |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 687 | std::list<shared_ptr<EthernetFace> > multicastFacesToRemove; |
| 688 | for (EthernetFactory::MulticastFaceMap::const_iterator i = |
| 689 | factory->getMulticastFaces().begin(); |
| 690 | i != factory->getMulticastFaces().end(); |
| 691 | ++i) |
| 692 | { |
| 693 | multicastFacesToRemove.push_back(i->second); |
| 694 | } |
| 695 | |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 696 | for (std::list<shared_ptr<NetworkInterfaceInfo> >::const_iterator i = nicList.begin(); |
| 697 | i != nicList.end(); |
| 698 | ++i) |
| 699 | { |
| 700 | const shared_ptr<NetworkInterfaceInfo>& nic = *i; |
| 701 | if (nic->isUp() && nic->isMulticastCapable()) |
| 702 | { |
| 703 | try |
| 704 | { |
| 705 | shared_ptr<EthernetFace> newFace = |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 706 | factory->createMulticastFace(nic, mcastGroup); |
Alexander Afanasyev | bbe3f0c | 2014-03-23 11:44:01 -0700 | [diff] [blame] | 707 | |
| 708 | addCreatedFaceToForwarder(newFace); |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 709 | multicastFacesToRemove.remove(newFace); |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 710 | } |
| 711 | catch (const EthernetFactory::Error& factoryError) |
| 712 | { |
| 713 | NFD_LOG_ERROR(factoryError.what() << ", continuing"); |
| 714 | } |
| 715 | catch (const EthernetFace::Error& faceError) |
| 716 | { |
| 717 | NFD_LOG_ERROR(faceError.what() << ", continuing"); |
| 718 | } |
| 719 | } |
| 720 | } |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 721 | |
| 722 | for (std::list<shared_ptr<EthernetFace> >::iterator i = |
| 723 | multicastFacesToRemove.begin(); |
| 724 | i != multicastFacesToRemove.end(); |
| 725 | ++i) |
| 726 | { |
| 727 | (*i)->close(); |
| 728 | } |
| 729 | } |
| 730 | else |
| 731 | { |
| 732 | std::list<shared_ptr<EthernetFace> > multicastFacesToRemove; |
| 733 | for (EthernetFactory::MulticastFaceMap::const_iterator i = |
| 734 | factory->getMulticastFaces().begin(); |
| 735 | i != factory->getMulticastFaces().end(); |
| 736 | ++i) |
| 737 | { |
| 738 | multicastFacesToRemove.push_back(i->second); |
| 739 | } |
| 740 | |
| 741 | for (std::list<shared_ptr<EthernetFace> >::iterator i = |
| 742 | multicastFacesToRemove.begin(); |
| 743 | i != multicastFacesToRemove.end(); |
| 744 | ++i) |
| 745 | { |
| 746 | (*i)->close(); |
| 747 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 748 | } |
| 749 | } |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 750 | #else |
| 751 | throw ConfigFile::Error("NFD was compiled without libpcap, cannot process \"ether\" section"); |
Alexander Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 752 | #endif // HAVE_LIBPCAP |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 755 | void |
| 756 | FaceManager::processSectionWebSocket(const ConfigSection& configSection, bool isDryRun) |
| 757 | { |
| 758 | // ; the websocket section contains settings of WebSocket faces and channels |
| 759 | // websocket |
| 760 | // { |
| 761 | // listen yes ; set to 'no' to disable WebSocket listener, default 'yes' |
| 762 | // port 9696 ; WebSocket listener port number |
| 763 | // enable_v4 yes ; set to 'no' to disable listening on IPv4 socket, default 'yes' |
| 764 | // enable_v6 yes ; set to 'no' to disable listening on IPv6 socket, default 'yes' |
| 765 | // } |
| 766 | |
| 767 | #if defined(HAVE_WEBSOCKET) |
| 768 | |
| 769 | std::string port = "9696"; |
| 770 | bool needToListen = true; |
| 771 | bool enableV4 = true; |
| 772 | bool enableV6 = true; |
| 773 | |
| 774 | for (ConfigSection::const_iterator i = configSection.begin(); |
| 775 | i != configSection.end(); |
| 776 | ++i) |
| 777 | { |
| 778 | if (i->first == "port") |
| 779 | { |
| 780 | port = i->second.get_value<std::string>(); |
| 781 | try |
| 782 | { |
| 783 | uint16_t portNo = boost::lexical_cast<uint16_t>(port); |
| 784 | NFD_LOG_TRACE("WebSocket port set to " << portNo); |
| 785 | } |
| 786 | catch (const std::bad_cast& error) |
| 787 | { |
| 788 | throw ConfigFile::Error("Invalid value for option " + |
| 789 | i->first + "\" in \"websocket\" section"); |
| 790 | } |
| 791 | } |
| 792 | else if (i->first == "listen") |
| 793 | { |
| 794 | needToListen = parseYesNo(i, i->first, "websocket"); |
| 795 | } |
| 796 | else if (i->first == "enable_v4") |
| 797 | { |
| 798 | enableV4 = parseYesNo(i, i->first, "websocket"); |
| 799 | } |
| 800 | else if (i->first == "enable_v6") |
| 801 | { |
| 802 | enableV6 = parseYesNo(i, i->first, "websocket"); |
| 803 | } |
| 804 | else |
| 805 | { |
| 806 | throw ConfigFile::Error("Unrecognized option \"" + |
| 807 | i->first + "\" in \"websocket\" section"); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | if (!enableV4 && !enableV6) |
| 812 | { |
| 813 | throw ConfigFile::Error("IPv4 and IPv6 channels have been disabled." |
| 814 | " Remove \"websocket\" section to disable WebSocket channels or" |
| 815 | " re-enable at least one channel type."); |
| 816 | } |
| 817 | |
| 818 | if (!enableV4 && enableV6) |
| 819 | { |
| 820 | throw ConfigFile::Error("NFD does not allow pure IPv6 WebSocket channel."); |
| 821 | } |
| 822 | |
| 823 | if (!isDryRun) |
| 824 | { |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 825 | if (m_factories.count("websocket") > 0) |
| 826 | { |
| 827 | return; |
| 828 | } |
| 829 | |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 830 | shared_ptr<WebSocketFactory> factory = ndn::make_shared<WebSocketFactory>(port); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 831 | m_factories.insert(std::make_pair("websocket", factory)); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 832 | |
| 833 | if (enableV6 && enableV4) |
| 834 | { |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 835 | shared_ptr<WebSocketChannel> ip46Channel = factory->createChannel("::", port); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 836 | if (needToListen) |
| 837 | { |
| 838 | ip46Channel->listen(bind(&FaceTable::add, &m_faceTable, _1)); |
| 839 | } |
| 840 | |
| 841 | m_factories.insert(std::make_pair("websocket46", factory)); |
| 842 | } |
| 843 | else if (enableV4) |
| 844 | { |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 845 | shared_ptr<WebSocketChannel> ipv4Channel = factory->createChannel("0.0.0.0", port); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 846 | if (needToListen) |
| 847 | { |
| 848 | ipv4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1)); |
| 849 | } |
| 850 | |
| 851 | m_factories.insert(std::make_pair("websocket4", factory)); |
| 852 | } |
| 853 | } |
| 854 | #else |
| 855 | throw ConfigFile::Error("NFD was compiled without WebSocket, " |
| 856 | "cannot process \"websocket\" section"); |
| 857 | #endif // HAVE_WEBSOCKET |
| 858 | } |
| 859 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 860 | |
| 861 | void |
| 862 | FaceManager::onFaceRequest(const Interest& request) |
| 863 | { |
| 864 | const Name& command = request.getName(); |
| 865 | const size_t commandNComps = command.size(); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 866 | const Name::Component& verb = command.get(COMMAND_PREFIX.size()); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 867 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 868 | UnsignedVerbDispatchTable::const_iterator unsignedVerbProcessor = |
| 869 | m_unsignedVerbDispatch.find(verb); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 870 | if (unsignedVerbProcessor != m_unsignedVerbDispatch.end()) |
| 871 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 872 | NFD_LOG_DEBUG("command result: processing verb: " << verb); |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 873 | (unsignedVerbProcessor->second)(this, request); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 874 | } |
| 875 | else if (COMMAND_UNSIGNED_NCOMPS <= commandNComps && |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 876 | commandNComps < COMMAND_SIGNED_NCOMPS) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 877 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 878 | NFD_LOG_DEBUG("command result: unsigned verb: " << command); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 879 | sendResponse(command, 401, "Signature required"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 880 | } |
| 881 | else if (commandNComps < COMMAND_SIGNED_NCOMPS || |
| 882 | !COMMAND_PREFIX.isPrefixOf(command)) |
| 883 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 884 | NFD_LOG_DEBUG("command result: malformed"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 885 | sendResponse(command, 400, "Malformed command"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 886 | } |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 887 | else |
| 888 | { |
| 889 | validate(request, |
| 890 | bind(&FaceManager::onValidatedFaceRequest, this, _1), |
| 891 | bind(&ManagerBase::onCommandValidationFailed, this, _1, _2)); |
| 892 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | void |
| 896 | FaceManager::onValidatedFaceRequest(const shared_ptr<const Interest>& request) |
| 897 | { |
| 898 | const Name& command = request->getName(); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 899 | const Name::Component& verb = command[COMMAND_PREFIX.size()]; |
| 900 | const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1]; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 901 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 902 | SignedVerbDispatchTable::const_iterator signedVerbProcessor = m_signedVerbDispatch.find(verb); |
| 903 | if (signedVerbProcessor != m_signedVerbDispatch.end()) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 904 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 905 | ControlParameters parameters; |
| 906 | if (!extractParameters(parameterComponent, parameters)) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 907 | { |
| 908 | sendResponse(command, 400, "Malformed command"); |
| 909 | return; |
| 910 | } |
| 911 | |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 912 | NFD_LOG_DEBUG("command result: processing verb: " << verb); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 913 | (signedVerbProcessor->second)(this, *request, parameters); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 914 | } |
| 915 | else |
| 916 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 917 | NFD_LOG_DEBUG("command result: unsupported verb: " << verb); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 918 | sendResponse(command, 501, "Unsupported command"); |
| 919 | } |
| 920 | |
| 921 | } |
| 922 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 923 | void |
Alexander Afanasyev | bbe3f0c | 2014-03-23 11:44:01 -0700 | [diff] [blame] | 924 | FaceManager::addCreatedFaceToForwarder(const shared_ptr<Face>& newFace) |
| 925 | { |
| 926 | m_faceTable.add(newFace); |
| 927 | |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 928 | //NFD_LOG_DEBUG("Created face " << newFace->getRemoteUri() << " ID " << newFace->getId()); |
Alexander Afanasyev | bbe3f0c | 2014-03-23 11:44:01 -0700 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | void |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 932 | FaceManager::onCreated(const Name& requestName, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 933 | ControlParameters& parameters, |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 934 | const shared_ptr<Face>& newFace) |
| 935 | { |
Alexander Afanasyev | bbe3f0c | 2014-03-23 11:44:01 -0700 | [diff] [blame] | 936 | addCreatedFaceToForwarder(newFace); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 937 | parameters.setFaceId(newFace->getId()); |
Steve DiBenedetto | 2599928 | 2014-05-22 15:25:12 -0600 | [diff] [blame] | 938 | parameters.setUri(newFace->getRemoteUri().toString()); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 939 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 940 | sendResponse(requestName, 200, "Success", parameters.wireEncode()); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | void |
| 944 | FaceManager::onConnectFailed(const Name& requestName, const std::string& reason) |
| 945 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 946 | NFD_LOG_DEBUG("Failed to create face: " << reason); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 947 | sendResponse(requestName, 408, reason); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | void |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 951 | FaceManager::createFace(const Interest& request, |
| 952 | ControlParameters& parameters) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 953 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 954 | const Name& requestName = request.getName(); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 955 | ndn::nfd::FaceCreateCommand command; |
| 956 | |
| 957 | if (!validateParameters(command, parameters)) |
| 958 | { |
| 959 | sendResponse(requestName, 400, "Malformed command"); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 960 | NFD_LOG_TRACE("invalid control parameters URI"); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 961 | return; |
| 962 | } |
| 963 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 964 | FaceUri uri; |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 965 | if (!uri.parse(parameters.getUri())) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 966 | { |
| 967 | sendResponse(requestName, 400, "Malformed command"); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 968 | NFD_LOG_TRACE("failed to parse URI"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 969 | return; |
| 970 | } |
| 971 | |
| 972 | FactoryMap::iterator factory = m_factories.find(uri.getScheme()); |
| 973 | if (factory == m_factories.end()) |
| 974 | { |
| 975 | sendResponse(requestName, 501, "Unsupported protocol"); |
| 976 | return; |
| 977 | } |
| 978 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 979 | try |
| 980 | { |
| 981 | factory->second->createFace(uri, |
| 982 | bind(&FaceManager::onCreated, |
| 983 | this, requestName, parameters, _1), |
| 984 | bind(&FaceManager::onConnectFailed, |
| 985 | this, requestName, _1)); |
| 986 | } |
| 987 | catch (const std::runtime_error& error) |
| 988 | { |
| 989 | std::string errorMessage = "NFD error: "; |
| 990 | errorMessage += error.what(); |
| 991 | |
| 992 | NFD_LOG_ERROR(errorMessage); |
| 993 | sendResponse(requestName, 500, errorMessage); |
| 994 | } |
| 995 | catch (const std::logic_error& error) |
| 996 | { |
| 997 | std::string errorMessage = "NFD error: "; |
| 998 | errorMessage += error.what(); |
| 999 | |
| 1000 | NFD_LOG_ERROR(errorMessage); |
| 1001 | sendResponse(requestName, 500, errorMessage); |
| 1002 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | |
| 1006 | void |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 1007 | FaceManager::destroyFace(const Interest& request, |
| 1008 | ControlParameters& parameters) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 1009 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 1010 | const Name& requestName = request.getName(); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 1011 | ndn::nfd::FaceDestroyCommand command; |
| 1012 | |
| 1013 | if (!validateParameters(command, parameters)) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 1014 | { |
| 1015 | sendResponse(requestName, 400, "Malformed command"); |
| 1016 | return; |
| 1017 | } |
| 1018 | |
| 1019 | shared_ptr<Face> target = m_faceTable.get(parameters.getFaceId()); |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 1020 | if (static_cast<bool>(target)) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 1021 | { |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 1022 | target->close(); |
| 1023 | } |
Steve DiBenedetto | ba74905 | 2014-03-22 19:54:53 -0600 | [diff] [blame] | 1024 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 1025 | sendResponse(requestName, 200, "Success", parameters.wireEncode()); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 1026 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 1029 | void |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 1030 | FaceManager::onAddFace(shared_ptr<Face> face) |
| 1031 | { |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 1032 | ndn::nfd::FaceEventNotification notification; |
| 1033 | notification.setKind(ndn::nfd::FACE_EVENT_CREATED) |
| 1034 | .setFaceId(face->getId()) |
| 1035 | .setRemoteUri(face->getRemoteUri().toString()) |
| 1036 | .setLocalUri(face->getLocalUri().toString()) |
| 1037 | .setFlags(getFaceFlags(*face)); |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 1038 | |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 1039 | m_notificationStream.postNotification(notification); |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | void |
| 1043 | FaceManager::onRemoveFace(shared_ptr<Face> face) |
| 1044 | { |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 1045 | ndn::nfd::FaceEventNotification notification; |
| 1046 | notification.setKind(ndn::nfd::FACE_EVENT_DESTROYED) |
| 1047 | .setFaceId(face->getId()) |
| 1048 | .setRemoteUri(face->getRemoteUri().toString()) |
| 1049 | .setLocalUri(face->getLocalUri().toString()) |
| 1050 | .setFlags(getFaceFlags(*face)); |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 1051 | |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 1052 | m_notificationStream.postNotification(notification); |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 1056 | bool |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 1057 | FaceManager::extractLocalControlParameters(const Interest& request, |
| 1058 | ControlParameters& parameters, |
| 1059 | ControlCommand& command, |
| 1060 | shared_ptr<LocalFace>& outFace, |
| 1061 | LocalControlFeature& outFeature) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 1062 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 1063 | if (!validateParameters(command, parameters)) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 1064 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 1065 | sendResponse(request.getName(), 400, "Malformed command"); |
| 1066 | return false; |
| 1067 | } |
| 1068 | |
| 1069 | shared_ptr<Face> face = m_faceTable.get(request.getIncomingFaceId()); |
| 1070 | |
| 1071 | if (!static_cast<bool>(face)) |
| 1072 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 1073 | NFD_LOG_DEBUG("command result: faceid " << request.getIncomingFaceId() << " not found"); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 1074 | sendResponse(request.getName(), 410, "Face not found"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 1075 | return false; |
| 1076 | } |
| 1077 | else if (!face->isLocal()) |
| 1078 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 1079 | NFD_LOG_DEBUG("command result: cannot enable local control on non-local faceid " << |
| 1080 | face->getId()); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 1081 | sendResponse(request.getName(), 412, "Face is non-local"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 1082 | return false; |
| 1083 | } |
| 1084 | |
| 1085 | outFace = dynamic_pointer_cast<LocalFace>(face); |
| 1086 | outFeature = static_cast<LocalControlFeature>(parameters.getLocalControlFeature()); |
| 1087 | |
| 1088 | return true; |
| 1089 | } |
| 1090 | |
| 1091 | void |
| 1092 | FaceManager::enableLocalControl(const Interest& request, |
| 1093 | ControlParameters& parameters) |
| 1094 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 1095 | ndn::nfd::FaceEnableLocalControlCommand command; |
| 1096 | |
| 1097 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 1098 | shared_ptr<LocalFace> face; |
| 1099 | LocalControlFeature feature; |
| 1100 | |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 1101 | if (extractLocalControlParameters(request, parameters, command, face, feature)) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 1102 | { |
| 1103 | face->setLocalControlHeaderFeature(feature, true); |
| 1104 | sendResponse(request.getName(), 200, "Success", parameters.wireEncode()); |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | void |
| 1109 | FaceManager::disableLocalControl(const Interest& request, |
| 1110 | ControlParameters& parameters) |
| 1111 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 1112 | ndn::nfd::FaceDisableLocalControlCommand command; |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 1113 | shared_ptr<LocalFace> face; |
| 1114 | LocalControlFeature feature; |
| 1115 | |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 1116 | if (extractLocalControlParameters(request, parameters, command, face, feature)) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 1117 | { |
| 1118 | face->setLocalControlHeaderFeature(feature, false); |
| 1119 | sendResponse(request.getName(), 200, "Success", parameters.wireEncode()); |
| 1120 | } |
| 1121 | } |
| 1122 | |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 1123 | void |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 1124 | FaceManager::listFaces(const Interest& request) |
| 1125 | { |
| 1126 | const Name& command = request.getName(); |
| 1127 | const size_t commandNComps = command.size(); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 1128 | |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 1129 | if (commandNComps < FACES_LIST_DATASET_NCOMPS || |
| 1130 | !FACES_LIST_DATASET_PREFIX.isPrefixOf(command)) |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 1131 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 1132 | NFD_LOG_DEBUG("command result: malformed"); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 1133 | sendResponse(command, 400, "Malformed command"); |
| 1134 | return; |
| 1135 | } |
| 1136 | |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 1137 | m_faceStatusPublisher.publish(); |
| 1138 | } |
| 1139 | |
| 1140 | void |
| 1141 | FaceManager::listChannels(const Interest& request) |
| 1142 | { |
| 1143 | NFD_LOG_DEBUG("in listChannels"); |
| 1144 | const Name& command = request.getName(); |
| 1145 | const size_t commandNComps = command.size(); |
| 1146 | |
| 1147 | if (commandNComps < CHANNELS_LIST_DATASET_NCOMPS || |
| 1148 | !CHANNELS_LIST_DATASET_PREFIX.isPrefixOf(command)) |
| 1149 | { |
| 1150 | NFD_LOG_DEBUG("command result: malformed"); |
| 1151 | sendResponse(command, 400, "Malformed command"); |
| 1152 | return; |
| 1153 | } |
| 1154 | |
| 1155 | NFD_LOG_DEBUG("publishing"); |
| 1156 | m_channelStatusPublisher.publish(); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 1157 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 1158 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 1159 | shared_ptr<ProtocolFactory> |
| 1160 | FaceManager::findFactory(const std::string& protocol) |
| 1161 | { |
| 1162 | FactoryMap::iterator factory = m_factories.find(protocol); |
| 1163 | if (factory != m_factories.end()) |
| 1164 | return factory->second; |
| 1165 | else |
| 1166 | return shared_ptr<ProtocolFactory>(); |
| 1167 | } |
| 1168 | |
| 1169 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 1170 | } // namespace nfd |