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