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 | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 33 | using namespace std; |
| 34 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 35 | NS_LOG_COMPONENT_DEFINE("ndn.TcpFace"); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 36 | |
| 37 | namespace ns3 { |
| 38 | namespace ndn { |
| 39 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 40 | class TcpBoundaryHeader : public Header { |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 41 | public: |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 42 | static TypeId |
| 43 | GetTypeId(void) |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 44 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 45 | static TypeId tid = |
| 46 | TypeId("ns3::ndn::TcpFace::BoundaryHeader").SetGroupName("Ndn").SetParent<Header>(); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 47 | return tid; |
| 48 | } |
| 49 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 50 | TcpBoundaryHeader() |
| 51 | : m_length(0) |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 52 | { |
| 53 | } |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 54 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 55 | TcpBoundaryHeader(Ptr<Packet> packet) |
| 56 | : m_length(packet->GetSize()) |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | TcpBoundaryHeader(uint32_t length) |
| 61 | : m_length(length) |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 62 | { |
| 63 | } |
| 64 | |
| 65 | uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 66 | GetLength() const |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 67 | { |
| 68 | return m_length; |
| 69 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 70 | |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 71 | virtual TypeId |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 72 | GetInstanceTypeId(void) const |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 73 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 74 | return TcpBoundaryHeader::GetTypeId(); |
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 | |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 77 | virtual void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 78 | Print(std::ostream& os) const |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 79 | { |
| 80 | os << "[" << m_length << "]"; |
| 81 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 82 | |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 83 | virtual uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 84 | GetSerializedSize(void) const |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 85 | { |
| 86 | return 4; |
| 87 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 88 | |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 89 | virtual void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 90 | Serialize(Buffer::Iterator start) const |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 91 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 92 | start.WriteU32(m_length); |
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 | |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 95 | virtual uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 96 | Deserialize(Buffer::Iterator start) |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 97 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 98 | m_length = start.ReadU32(); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 99 | return 4; |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | uint32_t m_length; |
| 104 | }; |
| 105 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 106 | NS_OBJECT_ENSURE_REGISTERED(TcpFace); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 107 | |
| 108 | TypeId |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 109 | TcpFace::GetTypeId() |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 110 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 111 | static TypeId tid = TypeId("ns3::ndn::TcpFace").SetParent<Face>().SetGroupName("Ndn"); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 112 | return tid; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * By default, Ndn face are created in the "down" state. Before |
| 117 | * becoming useable, the user must invoke SetUp on the face |
| 118 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 119 | TcpFace::TcpFace(Ptr<Node> node, Ptr<Socket> socket, Ipv4Address address) |
| 120 | : Face(node) |
| 121 | , m_socket(socket) |
| 122 | , m_address(address) |
| 123 | , m_pendingPacketLength(0) |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 124 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 125 | SetMetric(1); // default metric |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 128 | TcpFace::~TcpFace() |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 129 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 130 | NS_LOG_FUNCTION_NOARGS(); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | TcpFace& TcpFace::operator= (const TcpFace &) |
| 134 | { |
| 135 | return *this; |
| 136 | } |
| 137 | |
| 138 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 139 | TcpFace::RegisterProtocolHandlers(const InterestHandler& interestHandler, |
| 140 | const DataHandler& dataHandler) |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 141 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 142 | NS_LOG_FUNCTION(this); |
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 | Face::RegisterProtocolHandlers(interestHandler, dataHandler); |
| 145 | m_socket->SetRecvCallback(MakeCallback(&TcpFace::ReceiveFromTcp, this)); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Alexander Afanasyev | aa84fae | 2013-07-27 12:01:37 -0700 | [diff] [blame] | 148 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 149 | TcpFace::UnRegisterProtocolHandlers() |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 150 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 151 | m_socket->SetRecvCallback(MakeNullCallback<void, Ptr<Socket>>()); |
| 152 | Face::UnRegisterProtocolHandlers(); |
Alexander Afanasyev | aa84fae | 2013-07-27 12:01:37 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | bool |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 156 | TcpFace::Send(Ptr<Packet> packet) |
Alexander Afanasyev | aa84fae | 2013-07-27 12:01:37 -0700 | [diff] [blame] | 157 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 158 | if (!Face::Send(packet)) { |
| 159 | return false; |
| 160 | } |
Alexander Afanasyev | aa84fae | 2013-07-27 12:01:37 -0700 | [diff] [blame] | 161 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 162 | NS_LOG_FUNCTION(this << packet); |
| 163 | |
| 164 | Ptr<Packet> boundary = Create<Packet>(); |
| 165 | TcpBoundaryHeader hdr(packet); |
| 166 | boundary->AddHeader(hdr); |
| 167 | |
| 168 | m_socket->Send(boundary); |
| 169 | m_socket->Send(packet); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 170 | |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 175 | TcpFace::ReceiveFromTcp(Ptr<Socket> clientSocket) |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 176 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 177 | NS_LOG_FUNCTION(this << clientSocket); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 178 | TcpBoundaryHeader hdr; |
| 179 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 180 | if (m_pendingPacketLength > 0) { |
| 181 | if (clientSocket->GetRxAvailable() >= m_pendingPacketLength) { |
| 182 | Ptr<Packet> realPacket = clientSocket->Recv(m_pendingPacketLength, 0); |
| 183 | NS_LOG_DEBUG("+++ Expected " << m_pendingPacketLength << " bytes, got " |
| 184 | << realPacket->GetSize() << " bytes"); |
| 185 | if (realPacket == 0) |
| 186 | return; |
| 187 | |
| 188 | Receive(realPacket); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 189 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 190 | else |
| 191 | return; // still not ready |
| 192 | } |
| 193 | |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 194 | m_pendingPacketLength = 0; |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 195 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 196 | while (clientSocket->GetRxAvailable() >= hdr.GetSerializedSize()) { |
| 197 | Ptr<Packet> boundary = clientSocket->Recv(hdr.GetSerializedSize(), 0); |
| 198 | if (boundary == 0) |
| 199 | return; // no idea why it would happen... |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 200 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 201 | NS_LOG_DEBUG("Expected 4 bytes, got " << boundary->GetSize() << " bytes"); |
| 202 | |
| 203 | boundary->RemoveHeader(hdr); |
| 204 | NS_LOG_DEBUG("Header specifies length: " << hdr.GetLength()); |
| 205 | m_pendingPacketLength = hdr.GetLength(); |
| 206 | |
| 207 | if (clientSocket->GetRxAvailable() >= hdr.GetLength()) { |
| 208 | Ptr<Packet> realPacket = clientSocket->Recv(hdr.GetLength(), 0); |
| 209 | if (realPacket == 0) { |
| 210 | NS_LOG_DEBUG("Got nothing, but requested at least " << hdr.GetLength()); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | NS_LOG_DEBUG("Receiving data " << hdr.GetLength() << " bytes, got " << realPacket->GetSize() |
| 215 | << " bytes"); |
| 216 | |
| 217 | Receive(realPacket); |
| 218 | m_pendingPacketLength = 0; |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 219 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 220 | else { |
| 221 | return; |
| 222 | } |
| 223 | } |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 227 | TcpFace::OnTcpConnectionClosed(Ptr<Socket> socket) |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 228 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 229 | NS_LOG_FUNCTION(this << socket); |
| 230 | GetNode()->GetObject<IpFaceStack>()->DestroyTcpFace(this); |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | Ipv4Address |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 234 | TcpFace::GetAddress() const |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 235 | { |
| 236 | return m_address; |
| 237 | } |
| 238 | |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 239 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 240 | TcpFace::SetCreateCallback(Callback<void, Ptr<Face>> callback) |
Alexander Afanasyev | aa84fae | 2013-07-27 12:01:37 -0700 | [diff] [blame] | 241 | { |
| 242 | m_onCreateCallback = callback; |
| 243 | } |
| 244 | |
| 245 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 246 | TcpFace::OnConnect(Ptr<Socket> socket) |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 247 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 248 | NS_LOG_FUNCTION(this << 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 | Ptr<L3Protocol> ndn = GetNode()->GetObject<L3Protocol>(); |
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 | ndn->AddFace(this); |
| 253 | this->SetUp(true); |
Alexander Afanasyev | aa84fae | 2013-07-27 12:01:37 -0700 | [diff] [blame] | 254 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 255 | socket->SetCloseCallbacks(MakeCallback(&TcpFace::OnTcpConnectionClosed, this), |
| 256 | MakeCallback(&TcpFace::OnTcpConnectionClosed, this)); |
| 257 | |
| 258 | if (!m_onCreateCallback.IsNull()) { |
| 259 | m_onCreateCallback(this); |
| 260 | m_onCreateCallback = IpFaceStack::NULL_CREATE_CALLBACK; |
| 261 | } |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 262 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 263 | |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 264 | std::ostream& |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 265 | TcpFace::Print(std::ostream& os) const |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 266 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 267 | os << "dev=tcp(" << GetId() << ", " << m_address << ")"; |
Alexander Afanasyev | f4e2452 | 2013-06-24 14:11:57 -0700 | [diff] [blame] | 268 | return os; |
| 269 | } |
| 270 | |
| 271 | } // namespace ndn |
| 272 | } // namespace ns3 |