Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -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. |
| 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/>. |
| 24 | */ |
| 25 | |
| 26 | #include "websocket-transport.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 27 | #include "common/global.hpp" |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
| 30 | namespace face { |
| 31 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 32 | NFD_LOG_INIT(WebSocketTransport); |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 33 | |
Eric Newberry | 4a4ccfe | 2016-07-21 22:51:04 -0700 | [diff] [blame] | 34 | static bool |
| 35 | isLoopback(const boost::asio::ip::address& addr) |
| 36 | { |
| 37 | if (addr.is_loopback()) { |
| 38 | return true; |
| 39 | } |
| 40 | // Workaround for loopback IPv4-mapped IPv6 addresses |
| 41 | // see https://svn.boost.org/trac/boost/ticket/9084 |
| 42 | else if (addr.is_v6()) { |
| 43 | auto addr6 = addr.to_v6(); |
| 44 | if (addr6.is_v4_mapped()) { |
| 45 | return addr6.to_v4().is_loopback(); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return false; |
| 50 | } |
| 51 | |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 52 | WebSocketTransport::WebSocketTransport(websocketpp::connection_hdl hdl, |
| 53 | websocket::Server& server, |
| 54 | time::milliseconds pingInterval) |
| 55 | : m_handle(hdl) |
| 56 | , m_server(server) |
| 57 | , m_pingInterval(pingInterval) |
| 58 | { |
| 59 | const auto& sock = m_server.get_con_from_hdl(hdl)->get_socket(); |
| 60 | this->setLocalUri(FaceUri(sock.local_endpoint(), "ws")); |
| 61 | this->setRemoteUri(FaceUri(sock.remote_endpoint(), "wsclient")); |
| 62 | |
Eric Newberry | 4a4ccfe | 2016-07-21 22:51:04 -0700 | [diff] [blame] | 63 | if (isLoopback(sock.local_endpoint().address()) && |
| 64 | isLoopback(sock.remote_endpoint().address())) { |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 65 | this->setScope(ndn::nfd::FACE_SCOPE_LOCAL); |
Eric Newberry | 4a4ccfe | 2016-07-21 22:51:04 -0700 | [diff] [blame] | 66 | } |
| 67 | else { |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 68 | this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL); |
Eric Newberry | 4a4ccfe | 2016-07-21 22:51:04 -0700 | [diff] [blame] | 69 | } |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 70 | |
| 71 | this->setPersistency(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
| 72 | this->setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
| 73 | this->setMtu(MTU_UNLIMITED); |
| 74 | |
| 75 | this->schedulePing(); |
| 76 | |
Davide Pesavento | a681a24 | 2019-03-29 23:48:27 -0400 | [diff] [blame] | 77 | NFD_LOG_FACE_DEBUG("Creating transport"); |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 80 | void |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame^] | 81 | WebSocketTransport::doSend(const Block& packet, const EndpointId&) |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 82 | { |
| 83 | NFD_LOG_FACE_TRACE(__func__); |
| 84 | |
| 85 | websocketpp::lib::error_code error; |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame^] | 86 | m_server.send(m_handle, packet.wire(), packet.size(), |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 87 | websocketpp::frame::opcode::binary, error); |
| 88 | if (error) |
| 89 | return processErrorCode(error); |
| 90 | |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame^] | 91 | NFD_LOG_FACE_TRACE("Successfully sent: " << packet.size() << " bytes"); |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void |
| 95 | WebSocketTransport::receiveMessage(const std::string& msg) |
| 96 | { |
| 97 | NFD_LOG_FACE_TRACE("Received: " << msg.size() << " bytes"); |
| 98 | |
| 99 | bool isOk = false; |
| 100 | Block element; |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 101 | std::tie(isOk, element) = Block::fromBuffer(reinterpret_cast<const uint8_t*>(msg.data()), msg.size()); |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 102 | if (!isOk) { |
| 103 | NFD_LOG_FACE_WARN("Failed to parse message payload"); |
| 104 | return; |
| 105 | } |
| 106 | |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame^] | 107 | this->receive(element); |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void |
| 111 | WebSocketTransport::schedulePing() |
| 112 | { |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 113 | m_pingEventId = getScheduler().schedule(m_pingInterval, [this] { sendPing(); }); |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void |
| 117 | WebSocketTransport::sendPing() |
| 118 | { |
| 119 | NFD_LOG_FACE_TRACE(__func__); |
| 120 | |
| 121 | websocketpp::lib::error_code error; |
| 122 | m_server.ping(m_handle, "NFD-WebSocket", error); |
| 123 | if (error) |
| 124 | return processErrorCode(error); |
| 125 | |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 126 | ++this->nOutPings; |
| 127 | |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 128 | this->schedulePing(); |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | WebSocketTransport::handlePong() |
| 133 | { |
| 134 | NFD_LOG_FACE_TRACE(__func__); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 135 | |
| 136 | ++this->nInPongs; |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void |
| 140 | WebSocketTransport::handlePongTimeout() |
| 141 | { |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 142 | NFD_LOG_FACE_ERROR("Pong timeout"); |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 143 | this->setState(TransportState::FAILED); |
| 144 | doClose(); |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | WebSocketTransport::processErrorCode(const websocketpp::lib::error_code& error) |
| 149 | { |
| 150 | NFD_LOG_FACE_TRACE(__func__); |
| 151 | |
| 152 | if (getState() == TransportState::CLOSING || |
| 153 | getState() == TransportState::FAILED || |
| 154 | getState() == TransportState::CLOSED) |
| 155 | // transport is shutting down, ignore any errors |
| 156 | return; |
| 157 | |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 158 | NFD_LOG_FACE_ERROR("Send or ping operation failed: " << error.message()); |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 159 | this->setState(TransportState::FAILED); |
| 160 | doClose(); |
| 161 | } |
| 162 | |
| 163 | void |
| 164 | WebSocketTransport::doClose() |
| 165 | { |
| 166 | NFD_LOG_FACE_TRACE(__func__); |
| 167 | |
| 168 | m_pingEventId.cancel(); |
| 169 | |
| 170 | // use the non-throwing variant and ignore errors, if any |
| 171 | websocketpp::lib::error_code error; |
| 172 | m_server.close(m_handle, websocketpp::close::status::normal, "closed by NFD", error); |
| 173 | |
| 174 | this->setState(TransportState::CLOSED); |
| 175 | } |
| 176 | |
| 177 | } // namespace face |
| 178 | } // namespace nfd |