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