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