Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "face-manager.hpp" |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 8 | #include "face-flags.hpp" |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 9 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 10 | #include "core/logger.hpp" |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 11 | #include "core/face-uri.hpp" |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 12 | #include "core/network-interface.hpp" |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 13 | #include "fw/face-table.hpp" |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 14 | #include "face/tcp-factory.hpp" |
| 15 | #include "face/udp-factory.hpp" |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 16 | |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 17 | #include <ndn-cpp-dev/management/nfd-face-event-notification.hpp> |
| 18 | |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 19 | #ifdef HAVE_UNIX_SOCKETS |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 20 | #include "face/unix-stream-factory.hpp" |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 21 | #endif // HAVE_UNIX_SOCKETS |
| 22 | |
| 23 | #ifdef HAVE_PCAP |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 24 | #include "face/ethernet-factory.hpp" |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 25 | #endif // HAVE_PCAP |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 26 | |
| 27 | namespace nfd { |
| 28 | |
| 29 | NFD_LOG_INIT("FaceManager"); |
| 30 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 31 | const Name FaceManager::COMMAND_PREFIX("/localhost/nfd/faces"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 32 | |
| 33 | const size_t FaceManager::COMMAND_UNSIGNED_NCOMPS = |
| 34 | FaceManager::COMMAND_PREFIX.size() + |
| 35 | 1 + // verb |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 36 | 1; // verb parameters |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 37 | |
| 38 | const size_t FaceManager::COMMAND_SIGNED_NCOMPS = |
| 39 | FaceManager::COMMAND_UNSIGNED_NCOMPS + |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 40 | 4; // (timestamp, nonce, signed info tlv, signature tlv) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 41 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 42 | const FaceManager::SignedVerbAndProcessor FaceManager::SIGNED_COMMAND_VERBS[] = |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 43 | { |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 44 | SignedVerbAndProcessor( |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 45 | Name::Component("create"), |
| 46 | &FaceManager::createFace |
| 47 | ), |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 48 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 49 | SignedVerbAndProcessor( |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 50 | Name::Component("destroy"), |
| 51 | &FaceManager::destroyFace |
| 52 | ), |
| 53 | |
| 54 | SignedVerbAndProcessor( |
| 55 | Name::Component("enable-local-control"), |
| 56 | &FaceManager::enableLocalControl |
| 57 | ), |
| 58 | |
| 59 | SignedVerbAndProcessor( |
| 60 | Name::Component("disable-local-control"), |
| 61 | &FaceManager::disableLocalControl |
| 62 | ), |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 65 | const FaceManager::UnsignedVerbAndProcessor FaceManager::UNSIGNED_COMMAND_VERBS[] = |
| 66 | { |
| 67 | UnsignedVerbAndProcessor( |
| 68 | Name::Component("list"), |
| 69 | &FaceManager::listFaces |
| 70 | ), |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 71 | |
| 72 | UnsignedVerbAndProcessor( |
| 73 | Name::Component("events"), |
| 74 | &FaceManager::ignoreUnsignedVerb |
| 75 | ), |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | const Name FaceManager::LIST_COMMAND_PREFIX("/localhost/nfd/faces/list"); |
| 79 | const size_t FaceManager::LIST_COMMAND_NCOMPS = LIST_COMMAND_PREFIX.size(); |
| 80 | |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 81 | const Name FaceManager::EVENTS_COMMAND_PREFIX("/localhost/nfd/faces/events"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 82 | |
| 83 | FaceManager::FaceManager(FaceTable& faceTable, |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 84 | shared_ptr<InternalFace> face) |
| 85 | : ManagerBase(face, FACE_MANAGER_PRIVILEGE) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 86 | , m_faceTable(faceTable) |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 87 | , m_statusPublisher(m_faceTable, m_face, LIST_COMMAND_PREFIX) |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 88 | , m_notificationStream(m_face, EVENTS_COMMAND_PREFIX) |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 89 | , m_signedVerbDispatch(SIGNED_COMMAND_VERBS, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 90 | SIGNED_COMMAND_VERBS + |
| 91 | (sizeof(SIGNED_COMMAND_VERBS) / sizeof(SignedVerbAndProcessor))) |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 92 | , m_unsignedVerbDispatch(UNSIGNED_COMMAND_VERBS, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 93 | UNSIGNED_COMMAND_VERBS + |
| 94 | (sizeof(UNSIGNED_COMMAND_VERBS) / sizeof(UnsignedVerbAndProcessor))) |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 95 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 96 | { |
| 97 | face->setInterestFilter("/localhost/nfd/faces", |
| 98 | bind(&FaceManager::onFaceRequest, this, _2)); |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 99 | |
| 100 | m_faceTable.onAdd += bind(&FaceManager::onAddFace, this, _1); |
| 101 | m_faceTable.onRemove += bind(&FaceManager::onRemoveFace, this, _1); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 104 | FaceManager::~FaceManager() |
| 105 | { |
| 106 | |
| 107 | } |
| 108 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 109 | void |
| 110 | FaceManager::setConfigFile(ConfigFile& configFile) |
| 111 | { |
| 112 | configFile.addSectionHandler("face_system", |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 113 | bind(&FaceManager::onConfig, this, _1, _2, _3)); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | |
| 117 | void |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 118 | FaceManager::onConfig(const ConfigSection& configSection, |
| 119 | bool isDryRun, |
| 120 | const std::string& filename) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 121 | { |
| 122 | bool hasSeenUnix = false; |
| 123 | bool hasSeenTcp = false; |
| 124 | bool hasSeenUdp = false; |
| 125 | bool hasSeenEther = false; |
| 126 | |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 127 | const std::list<shared_ptr<NetworkInterfaceInfo> > nicList(listNetworkInterfaces()); |
| 128 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 129 | for (ConfigSection::const_iterator item = configSection.begin(); |
| 130 | item != configSection.end(); |
| 131 | ++item) |
| 132 | { |
| 133 | if (item->first == "unix") |
| 134 | { |
| 135 | if (hasSeenUnix) |
| 136 | throw Error("Duplicate \"unix\" section"); |
| 137 | hasSeenUnix = true; |
| 138 | |
| 139 | processSectionUnix(item->second, isDryRun); |
| 140 | } |
| 141 | else if (item->first == "tcp") |
| 142 | { |
| 143 | if (hasSeenTcp) |
| 144 | throw Error("Duplicate \"tcp\" section"); |
| 145 | hasSeenTcp = true; |
| 146 | |
| 147 | processSectionTcp(item->second, isDryRun); |
| 148 | } |
| 149 | else if (item->first == "udp") |
| 150 | { |
| 151 | if (hasSeenUdp) |
| 152 | throw Error("Duplicate \"udp\" section"); |
| 153 | hasSeenUdp = true; |
| 154 | |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 155 | processSectionUdp(item->second, isDryRun, nicList); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 156 | } |
| 157 | else if (item->first == "ether") |
| 158 | { |
| 159 | if (hasSeenEther) |
| 160 | throw Error("Duplicate \"ether\" section"); |
| 161 | hasSeenEther = true; |
| 162 | |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 163 | processSectionEther(item->second, isDryRun, nicList); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 164 | } |
| 165 | else |
| 166 | { |
| 167 | throw Error("Unrecognized option \"" + item->first + "\""); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | FaceManager::processSectionUnix(const ConfigSection& configSection, bool isDryRun) |
| 174 | { |
| 175 | // ; the unix section contains settings of UNIX stream faces and channels |
| 176 | // unix |
| 177 | // { |
| 178 | // listen yes ; set to 'no' to disable UNIX stream listener, default 'yes' |
| 179 | // path /var/run/nfd.sock ; UNIX stream listener path |
| 180 | // } |
| 181 | |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 182 | #if defined(HAVE_UNIX_SOCKETS) |
| 183 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 184 | bool needToListen = true; |
| 185 | std::string path = "/var/run/nfd.sock"; |
| 186 | |
| 187 | for (ConfigSection::const_iterator i = configSection.begin(); |
| 188 | i != configSection.end(); |
| 189 | ++i) |
| 190 | { |
| 191 | if (i->first == "path") |
| 192 | { |
| 193 | path = i->second.get_value<std::string>(); |
| 194 | } |
| 195 | else if (i->first == "listen") |
| 196 | { |
| 197 | needToListen = parseYesNo(i, i->first, "unix"); |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | throw ConfigFile::Error("Unrecognized option \"" + i->first + "\" in \"unix\" section"); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | if (!isDryRun) |
| 206 | { |
| 207 | shared_ptr<UnixStreamFactory> factory = make_shared<UnixStreamFactory>(); |
| 208 | shared_ptr<UnixStreamChannel> unixChannel = factory->createChannel(path); |
| 209 | |
| 210 | if (needToListen) |
| 211 | { |
| 212 | // Should acceptFailed callback be used somehow? |
| 213 | unixChannel->listen(bind(&FaceTable::add, &m_faceTable, _1), |
| 214 | UnixStreamChannel::ConnectFailedCallback()); |
| 215 | } |
| 216 | |
| 217 | m_factories.insert(std::make_pair("unix", factory)); |
| 218 | } |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 219 | #else |
| 220 | throw ConfigFile::Error("NFD was compiled without UNIX sockets support, cannot process \"unix\" section"); |
| 221 | #endif // HAVE_UNIX_SOCKETS |
| 222 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void |
| 226 | FaceManager::processSectionTcp(const ConfigSection& configSection, bool isDryRun) |
| 227 | { |
| 228 | // ; the tcp section contains settings of TCP faces and channels |
| 229 | // tcp |
| 230 | // { |
| 231 | // listen yes ; set to 'no' to disable TCP listener, default 'yes' |
| 232 | // port 6363 ; TCP listener port number |
| 233 | // } |
| 234 | |
| 235 | std::string port = "6363"; |
| 236 | bool needToListen = true; |
| 237 | |
| 238 | for (ConfigSection::const_iterator i = configSection.begin(); |
| 239 | i != configSection.end(); |
| 240 | ++i) |
| 241 | { |
| 242 | if (i->first == "port") |
| 243 | { |
| 244 | port = i->second.get_value<std::string>(); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 245 | try |
| 246 | { |
| 247 | uint16_t portNo = boost::lexical_cast<uint16_t>(port); |
| 248 | NFD_LOG_TRACE("TCP port set to " << portNo); |
| 249 | } |
| 250 | catch (const std::bad_cast& error) |
| 251 | { |
| 252 | throw ConfigFile::Error("Invalid value for option " + |
| 253 | i->first + "\" in \"udp\" section"); |
| 254 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 255 | } |
| 256 | else if (i->first == "listen") |
| 257 | { |
| 258 | needToListen = parseYesNo(i, i->first, "tcp"); |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | throw ConfigFile::Error("Unrecognized option \"" + i->first + "\" in \"tcp\" section"); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if (!isDryRun) |
| 267 | { |
| 268 | shared_ptr<TcpFactory> factory = make_shared<TcpFactory>(boost::cref(port)); |
| 269 | |
| 270 | using namespace boost::asio::ip; |
| 271 | |
| 272 | shared_ptr<TcpChannel> ipv4Channel = factory->createChannel("0.0.0.0", port); |
| 273 | shared_ptr<TcpChannel> ipv6Channel = factory->createChannel("::", port); |
| 274 | |
| 275 | if (needToListen) |
| 276 | { |
| 277 | // Should acceptFailed callback be used somehow? |
| 278 | |
| 279 | ipv4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), |
| 280 | TcpChannel::ConnectFailedCallback()); |
| 281 | ipv6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), |
| 282 | TcpChannel::ConnectFailedCallback()); |
| 283 | } |
| 284 | |
| 285 | m_factories.insert(std::make_pair("tcp", factory)); |
| 286 | m_factories.insert(std::make_pair("tcp4", factory)); |
| 287 | m_factories.insert(std::make_pair("tcp6", factory)); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | void |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 292 | FaceManager::processSectionUdp(const ConfigSection& configSection, |
| 293 | bool isDryRun, |
| 294 | const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 295 | { |
| 296 | // ; the udp section contains settings of UDP faces and channels |
| 297 | // udp |
| 298 | // { |
| 299 | // port 6363 ; UDP unicast port number |
| 300 | // idle_timeout 30 ; idle time (seconds) before closing a UDP unicast face |
| 301 | // keep_alive_interval 25; interval (seconds) between keep-alive refreshes |
| 302 | |
| 303 | // ; NFD creates one UDP multicast face per NIC |
| 304 | // mcast yes ; set to 'no' to disable UDP multicast, default 'yes' |
| 305 | // mcast_port 56363 ; UDP multicast port number |
| 306 | // mcast_group 224.0.23.170 ; UDP multicast group (IPv4 only) |
| 307 | // } |
| 308 | |
| 309 | std::string port = "6363"; |
| 310 | size_t timeout = 30; |
| 311 | size_t keepAliveInterval = 25; |
| 312 | bool useMcast = true; |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 313 | std::string mcastGroup = "224.0.23.170"; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 314 | std::string mcastPort = "56363"; |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 315 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 316 | |
| 317 | for (ConfigSection::const_iterator i = configSection.begin(); |
| 318 | i != configSection.end(); |
| 319 | ++i) |
| 320 | { |
| 321 | if (i->first == "port") |
| 322 | { |
| 323 | port = i->second.get_value<std::string>(); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 324 | try |
| 325 | { |
| 326 | uint16_t portNo = boost::lexical_cast<uint16_t>(port); |
| 327 | NFD_LOG_TRACE("UDP port set to " << portNo); |
| 328 | } |
| 329 | catch (const std::bad_cast& error) |
| 330 | { |
| 331 | throw ConfigFile::Error("Invalid value for option " + |
| 332 | i->first + "\" in \"udp\" section"); |
| 333 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 334 | } |
| 335 | else if (i->first == "idle_timeout") |
| 336 | { |
| 337 | try |
| 338 | { |
| 339 | timeout = i->second.get_value<size_t>(); |
| 340 | } |
| 341 | catch (const std::exception& e) |
| 342 | { |
| 343 | throw ConfigFile::Error("Invalid value for option \"" + |
| 344 | i->first + "\" in \"udp\" section"); |
| 345 | } |
| 346 | } |
| 347 | else if (i->first == "keep_alive_interval") |
| 348 | { |
| 349 | try |
| 350 | { |
| 351 | keepAliveInterval = i->second.get_value<size_t>(); |
Alexander Afanasyev | efea8fe | 2014-03-23 00:00:35 -0700 | [diff] [blame] | 352 | |
| 353 | /// \todo Make use of keepAliveInterval |
| 354 | (void)(keepAliveInterval); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 355 | } |
| 356 | catch (const std::exception& e) |
| 357 | { |
| 358 | throw ConfigFile::Error("Invalid value for option \"" + |
| 359 | i->first + "\" in \"udp\" section"); |
| 360 | } |
| 361 | } |
| 362 | else if (i->first == "mcast") |
| 363 | { |
| 364 | useMcast = parseYesNo(i, i->first, "udp"); |
| 365 | } |
| 366 | else if (i->first == "mcast_port") |
| 367 | { |
| 368 | mcastPort = i->second.get_value<std::string>(); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 369 | try |
| 370 | { |
| 371 | uint16_t portNo = boost::lexical_cast<uint16_t>(mcastPort); |
| 372 | NFD_LOG_TRACE("UDP multicast port set to " << portNo); |
| 373 | } |
| 374 | catch (const std::bad_cast& error) |
| 375 | { |
| 376 | throw ConfigFile::Error("Invalid value for option " + |
| 377 | i->first + "\" in \"udp\" section"); |
| 378 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 379 | } |
| 380 | else if (i->first == "mcast_group") |
| 381 | { |
| 382 | using namespace boost::asio::ip; |
| 383 | mcastGroup = i->second.get_value<std::string>(); |
| 384 | try |
| 385 | { |
| 386 | address mcastGroupTest = address::from_string(mcastGroup); |
| 387 | if (!mcastGroupTest.is_v4()) |
| 388 | { |
| 389 | throw ConfigFile::Error("Invalid value for option \"" + |
| 390 | i->first + "\" in \"udp\" section"); |
| 391 | } |
| 392 | } |
| 393 | catch(const std::runtime_error& e) |
| 394 | { |
| 395 | throw ConfigFile::Error("Invalid value for option \"" + |
| 396 | i->first + "\" in \"udp\" section"); |
| 397 | } |
| 398 | } |
| 399 | else |
| 400 | { |
| 401 | throw ConfigFile::Error("Unrecognized option \"" + i->first + "\" in \"udp\" section"); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /// \todo what is keep alive interval used for? |
| 406 | |
| 407 | if (!isDryRun) |
| 408 | { |
| 409 | shared_ptr<UdpFactory> factory = make_shared<UdpFactory>(boost::cref(port)); |
| 410 | |
Steve DiBenedetto | b9906cf | 2014-03-27 09:18:28 -0600 | [diff] [blame] | 411 | shared_ptr<UdpChannel> v4Channel = |
| 412 | factory->createChannel("0.0.0.0", port, time::seconds(timeout)); |
| 413 | |
| 414 | shared_ptr<UdpChannel> v6Channel = |
| 415 | factory->createChannel("::", port, time::seconds(timeout)); |
| 416 | |
| 417 | v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), |
| 418 | UdpChannel::ConnectFailedCallback()); |
| 419 | |
| 420 | v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), |
| 421 | UdpChannel::ConnectFailedCallback()); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 422 | |
| 423 | m_factories.insert(std::make_pair("udp", factory)); |
| 424 | m_factories.insert(std::make_pair("udp4", factory)); |
| 425 | m_factories.insert(std::make_pair("udp6", factory)); |
| 426 | |
| 427 | if (useMcast) |
| 428 | { |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 429 | for (std::list<shared_ptr<NetworkInterfaceInfo> >::const_iterator i = nicList.begin(); |
| 430 | i != nicList.end(); |
| 431 | ++i) |
| 432 | { |
| 433 | const shared_ptr<NetworkInterfaceInfo>& nic = *i; |
| 434 | if (nic->isUp() && nic->isMulticastCapable() && !nic->ipv4Addresses.empty()) |
| 435 | { |
| 436 | shared_ptr<MulticastUdpFace> newFace = |
| 437 | factory->createMulticastFace(nic->ipv4Addresses[0].to_string(), |
| 438 | mcastGroup, mcastPort); |
| 439 | |
Alexander Afanasyev | bbe3f0c | 2014-03-23 11:44:01 -0700 | [diff] [blame] | 440 | addCreatedFaceToForwarder(newFace); |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
| 447 | void |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 448 | FaceManager::processSectionEther(const ConfigSection& configSection, |
| 449 | bool isDryRun, |
| 450 | const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 451 | { |
| 452 | // ; the ether section contains settings of Ethernet faces and channels |
| 453 | // ether |
| 454 | // { |
| 455 | // ; NFD creates one Ethernet multicast face per NIC |
| 456 | // mcast yes ; set to 'no' to disable Ethernet multicast, default 'yes' |
| 457 | // mcast_group 01:00:5E:00:17:AA ; Ethernet multicast group |
| 458 | // } |
| 459 | |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 460 | #if defined(HAVE_PCAP) |
| 461 | |
| 462 | using ethernet::Address; |
| 463 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 464 | bool useMcast = true; |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 465 | Address mcastGroup(ethernet::getDefaultMulticastAddress()); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 466 | |
| 467 | for (ConfigSection::const_iterator i = configSection.begin(); |
| 468 | i != configSection.end(); |
| 469 | ++i) |
| 470 | { |
| 471 | if (i->first == "mcast") |
| 472 | { |
| 473 | useMcast = parseYesNo(i, i->first, "ether"); |
| 474 | } |
| 475 | |
| 476 | else if (i->first == "mcast_group") |
| 477 | { |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 478 | mcastGroup = Address::fromString(i->second.get_value<std::string>()); |
| 479 | if (mcastGroup.isNull()) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 480 | { |
| 481 | throw ConfigFile::Error("Invalid value for option \"" + |
| 482 | i->first + "\" in \"ether\" section"); |
| 483 | } |
| 484 | } |
| 485 | else |
| 486 | { |
| 487 | throw ConfigFile::Error("Unrecognized option \"" + i->first + "\" in \"ether\" section"); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | if (!isDryRun) |
| 492 | { |
| 493 | shared_ptr<EthernetFactory> factory = make_shared<EthernetFactory>(); |
| 494 | m_factories.insert(std::make_pair("ether", factory)); |
| 495 | |
| 496 | if (useMcast) |
| 497 | { |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 498 | for (std::list<shared_ptr<NetworkInterfaceInfo> >::const_iterator i = nicList.begin(); |
| 499 | i != nicList.end(); |
| 500 | ++i) |
| 501 | { |
| 502 | const shared_ptr<NetworkInterfaceInfo>& nic = *i; |
| 503 | if (nic->isUp() && nic->isMulticastCapable()) |
| 504 | { |
| 505 | try |
| 506 | { |
| 507 | shared_ptr<EthernetFace> newFace = |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 508 | factory->createMulticastFace(nic, mcastGroup); |
Alexander Afanasyev | bbe3f0c | 2014-03-23 11:44:01 -0700 | [diff] [blame] | 509 | |
| 510 | addCreatedFaceToForwarder(newFace); |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 511 | } |
| 512 | catch (const EthernetFactory::Error& factoryError) |
| 513 | { |
| 514 | NFD_LOG_ERROR(factoryError.what() << ", continuing"); |
| 515 | } |
| 516 | catch (const EthernetFace::Error& faceError) |
| 517 | { |
| 518 | NFD_LOG_ERROR(faceError.what() << ", continuing"); |
| 519 | } |
| 520 | } |
| 521 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 522 | } |
| 523 | } |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 524 | #else |
| 525 | throw ConfigFile::Error("NFD was compiled without libpcap, cannot process \"ether\" section"); |
| 526 | #endif // HAVE_PCAP |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | |
| 530 | void |
| 531 | FaceManager::onFaceRequest(const Interest& request) |
| 532 | { |
| 533 | const Name& command = request.getName(); |
| 534 | const size_t commandNComps = command.size(); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 535 | const Name::Component& verb = command.get(COMMAND_PREFIX.size()); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 536 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 537 | UnsignedVerbDispatchTable::const_iterator unsignedVerbProcessor = m_unsignedVerbDispatch.find(verb); |
| 538 | if (unsignedVerbProcessor != m_unsignedVerbDispatch.end()) |
| 539 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 540 | NFD_LOG_DEBUG("command result: processing verb: " << verb); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 541 | (unsignedVerbProcessor->second)(this, boost::cref(request)); |
| 542 | } |
| 543 | else if (COMMAND_UNSIGNED_NCOMPS <= commandNComps && |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 544 | commandNComps < COMMAND_SIGNED_NCOMPS) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 545 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 546 | NFD_LOG_DEBUG("command result: unsigned verb: " << command); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 547 | sendResponse(command, 401, "Signature required"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 548 | } |
| 549 | else if (commandNComps < COMMAND_SIGNED_NCOMPS || |
| 550 | !COMMAND_PREFIX.isPrefixOf(command)) |
| 551 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 552 | NFD_LOG_DEBUG("command result: malformed"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 553 | sendResponse(command, 400, "Malformed command"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 554 | } |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 555 | else |
| 556 | { |
| 557 | validate(request, |
| 558 | bind(&FaceManager::onValidatedFaceRequest, this, _1), |
| 559 | bind(&ManagerBase::onCommandValidationFailed, this, _1, _2)); |
| 560 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | void |
| 564 | FaceManager::onValidatedFaceRequest(const shared_ptr<const Interest>& request) |
| 565 | { |
| 566 | const Name& command = request->getName(); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 567 | const Name::Component& verb = command[COMMAND_PREFIX.size()]; |
| 568 | const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1]; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 569 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 570 | SignedVerbDispatchTable::const_iterator signedVerbProcessor = m_signedVerbDispatch.find(verb); |
| 571 | if (signedVerbProcessor != m_signedVerbDispatch.end()) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 572 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 573 | ControlParameters parameters; |
| 574 | if (!extractParameters(parameterComponent, parameters)) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 575 | { |
| 576 | sendResponse(command, 400, "Malformed command"); |
| 577 | return; |
| 578 | } |
| 579 | |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 580 | NFD_LOG_DEBUG("command result: processing verb: " << verb); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 581 | (signedVerbProcessor->second)(this, *request, parameters); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 582 | } |
| 583 | else |
| 584 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 585 | NFD_LOG_DEBUG("command result: unsupported verb: " << verb); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 586 | sendResponse(command, 501, "Unsupported command"); |
| 587 | } |
| 588 | |
| 589 | } |
| 590 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 591 | void |
Alexander Afanasyev | bbe3f0c | 2014-03-23 11:44:01 -0700 | [diff] [blame] | 592 | FaceManager::addCreatedFaceToForwarder(const shared_ptr<Face>& newFace) |
| 593 | { |
| 594 | m_faceTable.add(newFace); |
| 595 | |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 596 | //NFD_LOG_DEBUG("Created face " << newFace->getRemoteUri() << " ID " << newFace->getId()); |
Alexander Afanasyev | bbe3f0c | 2014-03-23 11:44:01 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | void |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 600 | FaceManager::onCreated(const Name& requestName, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 601 | ControlParameters& parameters, |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 602 | const shared_ptr<Face>& newFace) |
| 603 | { |
Alexander Afanasyev | bbe3f0c | 2014-03-23 11:44:01 -0700 | [diff] [blame] | 604 | addCreatedFaceToForwarder(newFace); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 605 | parameters.setFaceId(newFace->getId()); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 606 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 607 | sendResponse(requestName, 200, "Success", parameters.wireEncode()); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | void |
| 611 | FaceManager::onConnectFailed(const Name& requestName, const std::string& reason) |
| 612 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 613 | NFD_LOG_DEBUG("Failed to create face: " << reason); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 614 | sendResponse(requestName, 408, reason); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | void |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 618 | FaceManager::createFace(const Interest& request, |
| 619 | ControlParameters& parameters) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 620 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 621 | const Name& requestName = request.getName(); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 622 | ndn::nfd::FaceCreateCommand command; |
| 623 | |
| 624 | if (!validateParameters(command, parameters)) |
| 625 | { |
| 626 | sendResponse(requestName, 400, "Malformed command"); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 627 | NFD_LOG_TRACE("invalid control parameters URI"); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 628 | return; |
| 629 | } |
| 630 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 631 | FaceUri uri; |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 632 | if (!uri.parse(parameters.getUri())) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 633 | { |
| 634 | sendResponse(requestName, 400, "Malformed command"); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 635 | NFD_LOG_TRACE("failed to parse URI"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 636 | return; |
| 637 | } |
| 638 | |
| 639 | FactoryMap::iterator factory = m_factories.find(uri.getScheme()); |
| 640 | if (factory == m_factories.end()) |
| 641 | { |
| 642 | sendResponse(requestName, 501, "Unsupported protocol"); |
| 643 | return; |
| 644 | } |
| 645 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 646 | try |
| 647 | { |
| 648 | factory->second->createFace(uri, |
| 649 | bind(&FaceManager::onCreated, |
| 650 | this, requestName, parameters, _1), |
| 651 | bind(&FaceManager::onConnectFailed, |
| 652 | this, requestName, _1)); |
| 653 | } |
| 654 | catch (const std::runtime_error& error) |
| 655 | { |
| 656 | std::string errorMessage = "NFD error: "; |
| 657 | errorMessage += error.what(); |
| 658 | |
| 659 | NFD_LOG_ERROR(errorMessage); |
| 660 | sendResponse(requestName, 500, errorMessage); |
| 661 | } |
| 662 | catch (const std::logic_error& error) |
| 663 | { |
| 664 | std::string errorMessage = "NFD error: "; |
| 665 | errorMessage += error.what(); |
| 666 | |
| 667 | NFD_LOG_ERROR(errorMessage); |
| 668 | sendResponse(requestName, 500, errorMessage); |
| 669 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | |
| 673 | void |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 674 | FaceManager::destroyFace(const Interest& request, |
| 675 | ControlParameters& parameters) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 676 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 677 | const Name& requestName = request.getName(); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 678 | ndn::nfd::FaceDestroyCommand command; |
| 679 | |
| 680 | if (!validateParameters(command, parameters)) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 681 | { |
| 682 | sendResponse(requestName, 400, "Malformed command"); |
| 683 | return; |
| 684 | } |
| 685 | |
| 686 | shared_ptr<Face> target = m_faceTable.get(parameters.getFaceId()); |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 687 | if (static_cast<bool>(target)) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 688 | { |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 689 | target->close(); |
| 690 | } |
Steve DiBenedetto | ba74905 | 2014-03-22 19:54:53 -0600 | [diff] [blame] | 691 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 692 | sendResponse(requestName, 200, "Success", parameters.wireEncode()); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 693 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 696 | void |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 697 | FaceManager::onAddFace(shared_ptr<Face> face) |
| 698 | { |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 699 | ndn::nfd::FaceEventNotification notification; |
| 700 | notification.setKind(ndn::nfd::FACE_EVENT_CREATED) |
| 701 | .setFaceId(face->getId()) |
| 702 | .setRemoteUri(face->getRemoteUri().toString()) |
| 703 | .setLocalUri(face->getLocalUri().toString()) |
| 704 | .setFlags(getFaceFlags(*face)); |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 705 | |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 706 | m_notificationStream.postNotification(notification); |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | void |
| 710 | FaceManager::onRemoveFace(shared_ptr<Face> face) |
| 711 | { |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 712 | ndn::nfd::FaceEventNotification notification; |
| 713 | notification.setKind(ndn::nfd::FACE_EVENT_DESTROYED) |
| 714 | .setFaceId(face->getId()) |
| 715 | .setRemoteUri(face->getRemoteUri().toString()) |
| 716 | .setLocalUri(face->getLocalUri().toString()) |
| 717 | .setFlags(getFaceFlags(*face)); |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 718 | |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 719 | m_notificationStream.postNotification(notification); |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 723 | bool |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 724 | FaceManager::extractLocalControlParameters(const Interest& request, |
| 725 | ControlParameters& parameters, |
| 726 | ControlCommand& command, |
| 727 | shared_ptr<LocalFace>& outFace, |
| 728 | LocalControlFeature& outFeature) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 729 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 730 | if (!validateParameters(command, parameters)) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 731 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 732 | sendResponse(request.getName(), 400, "Malformed command"); |
| 733 | return false; |
| 734 | } |
| 735 | |
| 736 | shared_ptr<Face> face = m_faceTable.get(request.getIncomingFaceId()); |
| 737 | |
| 738 | if (!static_cast<bool>(face)) |
| 739 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 740 | NFD_LOG_DEBUG("command result: faceid " << request.getIncomingFaceId() << " not found"); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 741 | sendResponse(request.getName(), 410, "Face not found"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 742 | return false; |
| 743 | } |
| 744 | else if (!face->isLocal()) |
| 745 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 746 | NFD_LOG_DEBUG("command result: cannot enable local control on non-local faceid " << |
| 747 | face->getId()); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 748 | sendResponse(request.getName(), 412, "Face is non-local"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 749 | return false; |
| 750 | } |
| 751 | |
| 752 | outFace = dynamic_pointer_cast<LocalFace>(face); |
| 753 | outFeature = static_cast<LocalControlFeature>(parameters.getLocalControlFeature()); |
| 754 | |
| 755 | return true; |
| 756 | } |
| 757 | |
| 758 | void |
| 759 | FaceManager::enableLocalControl(const Interest& request, |
| 760 | ControlParameters& parameters) |
| 761 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 762 | ndn::nfd::FaceEnableLocalControlCommand command; |
| 763 | |
| 764 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 765 | shared_ptr<LocalFace> face; |
| 766 | LocalControlFeature feature; |
| 767 | |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 768 | if (extractLocalControlParameters(request, parameters, command, face, feature)) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 769 | { |
| 770 | face->setLocalControlHeaderFeature(feature, true); |
| 771 | sendResponse(request.getName(), 200, "Success", parameters.wireEncode()); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | void |
| 776 | FaceManager::disableLocalControl(const Interest& request, |
| 777 | ControlParameters& parameters) |
| 778 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 779 | ndn::nfd::FaceDisableLocalControlCommand command; |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 780 | shared_ptr<LocalFace> face; |
| 781 | LocalControlFeature feature; |
| 782 | |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 783 | if (extractLocalControlParameters(request, parameters, command, face, feature)) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 784 | { |
| 785 | face->setLocalControlHeaderFeature(feature, false); |
| 786 | sendResponse(request.getName(), 200, "Success", parameters.wireEncode()); |
| 787 | } |
| 788 | } |
| 789 | |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 790 | void |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 791 | FaceManager::listFaces(const Interest& request) |
| 792 | { |
| 793 | const Name& command = request.getName(); |
| 794 | const size_t commandNComps = command.size(); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 795 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 796 | if (commandNComps < LIST_COMMAND_NCOMPS || |
| 797 | !LIST_COMMAND_PREFIX.isPrefixOf(command)) |
| 798 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 799 | NFD_LOG_DEBUG("command result: malformed"); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 800 | sendResponse(command, 400, "Malformed command"); |
| 801 | return; |
| 802 | } |
| 803 | |
| 804 | m_statusPublisher.publish(); |
| 805 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 806 | |
| 807 | } // namespace nfd |