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