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