Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * |
| 20 | */ |
| 21 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 22 | #include "ndn-tcp-face.hpp" |
| 23 | #include "ndn-ip-face-stack.hpp" |
Alexander Afanasyev | d573af2 | 2013-07-27 12:57:08 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 25 | #include "ns3/ndn-l3-protocol.hpp" |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 26 | |
| 27 | #include "ns3/log.h" |
| 28 | #include "ns3/packet.h" |
| 29 | #include "ns3/node.h" |
| 30 | #include "ns3/pointer.h" |
| 31 | #include "ns3/tcp-socket-factory.h" |
| 32 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 33 | #include "ns3/ndn-name.hpp" |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 34 | |
| 35 | using namespace std; |
| 36 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 37 | NS_LOG_COMPONENT_DEFINE("ndn.TcpFace"); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 38 | |
| 39 | namespace ns3 { |
| 40 | namespace ndn { |
| 41 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 42 | class TcpBoundaryHeader : public Header { |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 43 | public: |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 44 | static TypeId |
| 45 | GetTypeId(void) |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 46 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 47 | static TypeId tid = |
| 48 | TypeId("ns3::ndn::TcpFace::BoundaryHeader").SetGroupName("Ndn").SetParent<Header>(); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 49 | return tid; |
| 50 | } |
| 51 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 52 | TcpBoundaryHeader() |
| 53 | : m_length(0) |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 54 | { |
| 55 | } |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 56 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 57 | TcpBoundaryHeader(Ptr<Packet> packet) |
| 58 | : m_length(packet->GetSize()) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | TcpBoundaryHeader(uint32_t length) |
| 63 | : m_length(length) |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 64 | { |
| 65 | } |
| 66 | |
| 67 | uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 68 | GetLength() const |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 69 | { |
| 70 | return m_length; |
| 71 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 72 | |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 73 | virtual TypeId |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 74 | GetInstanceTypeId(void) const |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 75 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 76 | return TcpBoundaryHeader::GetTypeId(); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 77 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 78 | |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 79 | virtual void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 80 | Print(std::ostream& os) const |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 81 | { |
| 82 | os << "[" << m_length << "]"; |
| 83 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 84 | |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 85 | virtual uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 86 | GetSerializedSize(void) const |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 87 | { |
| 88 | return 4; |
| 89 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 90 | |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 91 | virtual void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 92 | Serialize(Buffer::Iterator start) const |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 93 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 94 | start.WriteU32(m_length); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 95 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 96 | |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 97 | virtual uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 98 | Deserialize(Buffer::Iterator start) |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 99 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 100 | m_length = start.ReadU32(); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 101 | return 4; |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | uint32_t m_length; |
| 106 | }; |
| 107 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 108 | NS_OBJECT_ENSURE_REGISTERED(TcpFace); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 109 | |
| 110 | TypeId |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 111 | TcpFace::GetTypeId() |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 112 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 113 | static TypeId tid = TypeId("ns3::ndn::TcpFace").SetParent<Face>().SetGroupName("Ndn"); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 114 | return tid; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * By default, Ndn face are created in the "down" state. Before |
| 119 | * becoming useable, the user must invoke SetUp on the face |
| 120 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 121 | TcpFace::TcpFace(Ptr<Node> node, Ptr<Socket> socket, Ipv4Address address) |
| 122 | : Face(node) |
| 123 | , m_socket(socket) |
| 124 | , m_address(address) |
| 125 | , m_pendingPacketLength(0) |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 126 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 127 | SetMetric(1); // default metric |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 130 | TcpFace::~TcpFace() |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 131 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 132 | NS_LOG_FUNCTION_NOARGS(); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | TcpFace& TcpFace::operator= (const TcpFace &) |
| 136 | { |
| 137 | return *this; |
| 138 | } |
| 139 | |
| 140 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 141 | TcpFace::RegisterProtocolHandlers(const InterestHandler& interestHandler, |
| 142 | const DataHandler& dataHandler) |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 143 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 144 | NS_LOG_FUNCTION(this); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 145 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 146 | Face::RegisterProtocolHandlers(interestHandler, dataHandler); |
| 147 | m_socket->SetRecvCallback(MakeCallback(&TcpFace::ReceiveFromTcp, this)); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Alexander Afanasyev | aa84fae | 2013-07-27 12:01:37 -0700 | [diff] [blame] | 150 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 151 | TcpFace::UnRegisterProtocolHandlers() |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 152 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 153 | m_socket->SetRecvCallback(MakeNullCallback<void, Ptr<Socket>>()); |
| 154 | Face::UnRegisterProtocolHandlers(); |
Alexander Afanasyev | aa84fae | 2013-07-27 12:01:37 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | bool |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 158 | TcpFace::Send(Ptr<Packet> packet) |
Alexander Afanasyev | aa84fae | 2013-07-27 12:01:37 -0700 | [diff] [blame] | 159 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 160 | if (!Face::Send(packet)) { |
| 161 | return false; |
| 162 | } |
Alexander Afanasyev | aa84fae | 2013-07-27 12:01:37 -0700 | [diff] [blame] | 163 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 164 | NS_LOG_FUNCTION(this << packet); |
| 165 | |
| 166 | Ptr<Packet> boundary = Create<Packet>(); |
| 167 | TcpBoundaryHeader hdr(packet); |
| 168 | boundary->AddHeader(hdr); |
| 169 | |
| 170 | m_socket->Send(boundary); |
| 171 | m_socket->Send(packet); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 172 | |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 177 | TcpFace::ReceiveFromTcp(Ptr<Socket> clientSocket) |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 178 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 179 | NS_LOG_FUNCTION(this << clientSocket); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 180 | TcpBoundaryHeader hdr; |
| 181 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 182 | if (m_pendingPacketLength > 0) { |
| 183 | if (clientSocket->GetRxAvailable() >= m_pendingPacketLength) { |
| 184 | Ptr<Packet> realPacket = clientSocket->Recv(m_pendingPacketLength, 0); |
| 185 | NS_LOG_DEBUG("+++ Expected " << m_pendingPacketLength << " bytes, got " |
| 186 | << realPacket->GetSize() << " bytes"); |
| 187 | if (realPacket == 0) |
| 188 | return; |
| 189 | |
| 190 | Receive(realPacket); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 191 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 192 | else |
| 193 | return; // still not ready |
| 194 | } |
| 195 | |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 196 | m_pendingPacketLength = 0; |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 197 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 198 | while (clientSocket->GetRxAvailable() >= hdr.GetSerializedSize()) { |
| 199 | Ptr<Packet> boundary = clientSocket->Recv(hdr.GetSerializedSize(), 0); |
| 200 | if (boundary == 0) |
| 201 | return; // no idea why it would happen... |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 202 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 203 | NS_LOG_DEBUG("Expected 4 bytes, got " << boundary->GetSize() << " bytes"); |
| 204 | |
| 205 | boundary->RemoveHeader(hdr); |
| 206 | NS_LOG_DEBUG("Header specifies length: " << hdr.GetLength()); |
| 207 | m_pendingPacketLength = hdr.GetLength(); |
| 208 | |
| 209 | if (clientSocket->GetRxAvailable() >= hdr.GetLength()) { |
| 210 | Ptr<Packet> realPacket = clientSocket->Recv(hdr.GetLength(), 0); |
| 211 | if (realPacket == 0) { |
| 212 | NS_LOG_DEBUG("Got nothing, but requested at least " << hdr.GetLength()); |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | NS_LOG_DEBUG("Receiving data " << hdr.GetLength() << " bytes, got " << realPacket->GetSize() |
| 217 | << " bytes"); |
| 218 | |
| 219 | Receive(realPacket); |
| 220 | m_pendingPacketLength = 0; |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 221 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 222 | else { |
| 223 | return; |
| 224 | } |
| 225 | } |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 229 | TcpFace::OnTcpConnectionClosed(Ptr<Socket> socket) |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 230 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 231 | NS_LOG_FUNCTION(this << socket); |
| 232 | GetNode()->GetObject<IpFaceStack>()->DestroyTcpFace(this); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | Ipv4Address |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 236 | TcpFace::GetAddress() const |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 237 | { |
| 238 | return m_address; |
| 239 | } |
| 240 | |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 241 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 242 | TcpFace::SetCreateCallback(Callback<void, Ptr<Face>> callback) |
Alexander Afanasyev | aa84fae | 2013-07-27 12:01:37 -0700 | [diff] [blame] | 243 | { |
| 244 | m_onCreateCallback = callback; |
| 245 | } |
| 246 | |
| 247 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 248 | TcpFace::OnConnect(Ptr<Socket> socket) |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 249 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 250 | NS_LOG_FUNCTION(this << socket); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 251 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 252 | Ptr<L3Protocol> ndn = GetNode()->GetObject<L3Protocol>(); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 253 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 254 | ndn->AddFace(this); |
| 255 | this->SetUp(true); |
Alexander Afanasyev | aa84fae | 2013-07-27 12:01:37 -0700 | [diff] [blame] | 256 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 257 | socket->SetCloseCallbacks(MakeCallback(&TcpFace::OnTcpConnectionClosed, this), |
| 258 | MakeCallback(&TcpFace::OnTcpConnectionClosed, this)); |
| 259 | |
| 260 | if (!m_onCreateCallback.IsNull()) { |
| 261 | m_onCreateCallback(this); |
| 262 | m_onCreateCallback = IpFaceStack::NULL_CREATE_CALLBACK; |
| 263 | } |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 264 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 265 | |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 266 | std::ostream& |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 267 | TcpFace::Print(std::ostream& os) const |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 268 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 269 | os << "dev=tcp(" << GetId() << ", " << m_address << ")"; |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 270 | return os; |
| 271 | } |
| 272 | |
| 273 | } // namespace ndn |
| 274 | } // namespace ns3 |