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