Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 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. |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -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 | */ |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 25 | |
| 26 | #include "websocket-factory.hpp" |
| 27 | |
| 28 | namespace nfd { |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 29 | namespace face { |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 31 | namespace ip = boost::asio::ip; |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 32 | |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 33 | NFD_LOG_INIT("WebSocketFactory"); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 34 | NFD_REGISTER_PROTOCOL_FACTORY(WebSocketFactory); |
| 35 | |
| 36 | const std::string& |
| 37 | WebSocketFactory::getId() |
| 38 | { |
| 39 | static std::string id("websocket"); |
| 40 | return id; |
| 41 | } |
| 42 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame^] | 43 | WebSocketFactory::WebSocketFactory(const CtorParams& params) |
| 44 | : ProtocolFactory(params) |
| 45 | { |
| 46 | } |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 47 | |
| 48 | void |
| 49 | WebSocketFactory::processConfig(OptionalConfigSection configSection, |
| 50 | FaceSystem::ConfigContext& context) |
| 51 | { |
| 52 | // websocket |
| 53 | // { |
| 54 | // listen yes |
| 55 | // port 9696 |
| 56 | // enable_v4 yes |
| 57 | // enable_v6 yes |
| 58 | // } |
| 59 | |
| 60 | bool wantListen = false; |
| 61 | uint16_t port = 9696; |
| 62 | bool enableV4 = true; |
| 63 | bool enableV6 = true; |
| 64 | |
| 65 | if (configSection) { |
| 66 | wantListen = true; |
| 67 | for (const auto& pair : *configSection) { |
| 68 | const std::string& key = pair.first; |
| 69 | |
| 70 | if (key == "listen") { |
| 71 | wantListen = ConfigFile::parseYesNo(pair, "face_system.websocket"); |
| 72 | } |
| 73 | else if (key == "port") { |
| 74 | port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.websocket"); |
| 75 | } |
| 76 | else if (key == "enable_v4") { |
| 77 | enableV4 = ConfigFile::parseYesNo(pair, "face_system.websocket"); |
| 78 | } |
| 79 | else if (key == "enable_v6") { |
| 80 | enableV6 = ConfigFile::parseYesNo(pair, "face_system.websocket"); |
| 81 | } |
| 82 | else { |
| 83 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.websocket." + key)); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if (!enableV4 && !enableV6) { |
| 89 | BOOST_THROW_EXCEPTION(ConfigFile::Error( |
| 90 | "IPv4 and IPv6 WebSocket channels have been disabled. Remove face_system.websocket section " |
| 91 | "to disable WebSocket channels or enable at least one channel type.")); |
| 92 | } |
| 93 | |
| 94 | if (!enableV4 && enableV6) { |
| 95 | // websocketpp's IPv6 socket always accepts IPv4 connections. |
| 96 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD does not allow pure IPv6 WebSocket channel.")); |
| 97 | } |
| 98 | |
| 99 | if (!context.isDryRun) { |
| 100 | if (!wantListen) { |
| 101 | if (!m_channels.empty()) { |
| 102 | NFD_LOG_WARN("Cannot close WebSocket channel after initialization"); |
| 103 | } |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | BOOST_ASSERT(enableV4); |
| 108 | websocket::Endpoint endpoint(enableV6 ? ip::tcp::v6() : ip::tcp::v4(), port); |
| 109 | |
| 110 | auto channel = this->createChannel(endpoint); |
| 111 | if (!channel->isListening()) { |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 112 | channel->listen(this->addFace); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 113 | if (m_channels.size() > 1) { |
| 114 | NFD_LOG_WARN("Adding WebSocket channel for new endpoint; cannot close existing channels"); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 121 | WebSocketFactory::createFace(const FaceUri& remoteUri, |
| 122 | const ndn::optional<FaceUri>& localUri, |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 123 | ndn::nfd::FacePersistency persistency, |
| 124 | bool wantLocalFieldsEnabled, |
| 125 | const FaceCreatedCallback& onCreated, |
| 126 | const FaceCreationFailedCallback& onFailure) |
| 127 | { |
| 128 | onFailure(406, "Unsupported protocol"); |
| 129 | } |
| 130 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 131 | shared_ptr<WebSocketChannel> |
| 132 | WebSocketFactory::createChannel(const websocket::Endpoint& endpoint) |
| 133 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 134 | auto channel = findChannel(endpoint); |
| 135 | if (channel) |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 136 | return channel; |
| 137 | |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 138 | channel = make_shared<WebSocketChannel>(endpoint); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 139 | m_channels[endpoint] = channel; |
| 140 | |
| 141 | return channel; |
| 142 | } |
| 143 | |
| 144 | shared_ptr<WebSocketChannel> |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 145 | WebSocketFactory::createChannel(const std::string& localIp, const std::string& localPort) |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 146 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 147 | websocket::Endpoint endpoint(ip::address::from_string(localIp), |
| 148 | boost::lexical_cast<uint16_t>(localPort)); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 149 | return createChannel(endpoint); |
| 150 | } |
| 151 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 152 | std::vector<shared_ptr<const Channel>> |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 153 | WebSocketFactory::getChannels() const |
| 154 | { |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 155 | return getChannelsFromMap(m_channels); |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 156 | } |
| 157 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 158 | shared_ptr<WebSocketChannel> |
| 159 | WebSocketFactory::findChannel(const websocket::Endpoint& endpoint) const |
| 160 | { |
| 161 | auto i = m_channels.find(endpoint); |
| 162 | if (i != m_channels.end()) |
| 163 | return i->second; |
| 164 | else |
| 165 | return nullptr; |
| 166 | } |
| 167 | |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 168 | } // namespace face |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 169 | } // namespace nfd |