NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
ndn-ip-face-stack.cc
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 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 #include "ndn-ip-face-stack.h"
22 #include "ndn-tcp-face.h"
23 #include "ndn-udp-face.h"
24 
25 #include "ns3/ndn-l3-protocol.h"
26 
27 #include "ns3/log.h"
28 #include "ns3/assert.h"
29 #include "ns3/packet.h"
30 #include "ns3/boolean.h"
31 
32 #include "ns3/socket.h"
33 #include "ns3/tcp-socket-factory.h"
34 #include "ns3/udp-socket-factory.h"
35 #include "ns3/simulator.h"
36 
37 NS_LOG_COMPONENT_DEFINE ("ndn.IpFaceStack");
38 
39 namespace ns3 {
40 namespace ndn {
41 
42 NS_OBJECT_ENSURE_REGISTERED (IpFaceStack);
43 
44 const Callback< void, Ptr<Face> > IpFaceStack::NULL_CREATE_CALLBACK = MakeNullCallback< void, Ptr<Face> > ();
45 
46 TypeId
47 IpFaceStack::GetTypeId (void)
48 {
49  static TypeId tid = TypeId ("ns3::ndn::IpFaceStack")
50  .SetGroupName ("Ndn")
51  .SetParent<Object> ()
52  .AddConstructor<IpFaceStack> ()
53 
54  .AddAttribute ("EnableTCP", "Enable ability to create TCP faces",
55  BooleanValue (true),
56  MakeBooleanAccessor (&IpFaceStack::m_enableTcp),
57  MakeBooleanChecker ())
58 
59  .AddAttribute ("EnableUDP", "Enable ability to create UDP faces",
60  BooleanValue (true),
61  MakeBooleanAccessor (&IpFaceStack::m_enableUdp),
62  MakeBooleanChecker ())
63  ;
64  return tid;
65 }
66 
68 {
69 }
70 
71 IpFaceStack::~IpFaceStack ()
72 {
73 }
74 
75 void
76 IpFaceStack::NotifyNewAggregate ()
77 {
78  if (m_node == 0)
79  {
80  m_node = GetObject<Node> ();
81  if (m_node != 0)
82  {
83  Simulator::ScheduleWithContext (m_node->GetId (), Seconds (0.1), &IpFaceStack::StartServer, this);
84  }
85  }
86 }
87 
88 // Application Methods
89 void
90 IpFaceStack::StartServer () // Called at time specified by Start
91 {
92  NS_LOG_FUNCTION (this);
93 
94  if (m_enableTcp)
95  {
96  m_tcpServer = Socket::CreateSocket (m_node, TcpSocketFactory::GetTypeId ());
97 
98  m_tcpServer->Bind (InetSocketAddress (Ipv4Address::GetAny (), L3Protocol::IP_STACK_PORT));
99  m_tcpServer->Listen ();
100 
101  m_tcpServer->SetAcceptCallback (MakeCallback (&IpFaceStack::OnTcpConnectionRequest, this),
102  MakeCallback (&IpFaceStack::OnTcpConnectionAccept, this));
103  }
104 
105  if (m_enableUdp)
106  {
107  m_udpServer = Socket::CreateSocket (m_node, UdpSocketFactory::GetTypeId ());
108  m_udpServer->Bind (InetSocketAddress (Ipv4Address::GetAny (), L3Protocol::IP_STACK_PORT));
109 
110  m_udpServer->SetRecvCallback (MakeCallback (&IpFaceStack::OnUdpPacket, this));
111  }
112 }
113 
114 bool
115 IpFaceStack::OnTcpConnectionRequest (Ptr< Socket > sock, const Address &addr)
116 {
117  NS_LOG_FUNCTION (this << sock << InetSocketAddress::ConvertFrom (addr));
118  return true; // accept all connections from anybody
119 }
120 
121 void
122 IpFaceStack::OnTcpConnectionAccept (Ptr<Socket> socket, const Address &addr)
123 {
124  NS_LOG_FUNCTION (this << socket << InetSocketAddress::ConvertFrom (addr));
125 
126  Ptr<L3Protocol> ndn = m_node->GetObject<L3Protocol> ();
127  Ptr<TcpFace> face = CreateObject<TcpFace> (m_node, socket, InetSocketAddress::ConvertFrom (addr).GetIpv4 ());
128 
129  ndn->AddFace (face);
130  face->SetUp (true);
131 
132  socket->SetCloseCallbacks (MakeCallback (&TcpFace::OnTcpConnectionClosed, face),
133  MakeCallback (&TcpFace::OnTcpConnectionClosed, face));
134 }
135 
136 void
137 IpFaceStack::OnUdpPacket (Ptr< Socket > socket)
138 {
139  NS_LOG_FUNCTION (this << socket);
140 
141  Ptr<Packet> packet;
142  Address from;
143  while ((packet = socket->RecvFrom (from)))
144  {
145  Ptr<UdpFace> face = CreateOrGetUdpFace (InetSocketAddress::ConvertFrom (from).GetIpv4 ());
146  face->ReceiveFromUdp (packet);
147  }
148 }
149 
150 Ptr<TcpFace>
151 IpFaceStack::GetTcpFaceByAddress (const Ipv4Address &address)
152 {
153  TcpFaceMap::iterator i = m_tcpFaceMap.find (address);
154  if (i != m_tcpFaceMap.end ())
155  return i->second;
156  else
157  return 0;
158 }
159 
160 void
161 IpFaceStack::DestroyTcpFace (Ptr<TcpFace> face)
162 {
163  m_tcpFaceMap.erase (face->GetAddress ());
164 }
165 
166 Ptr<UdpFace>
167 IpFaceStack::GetUdpFaceByAddress (const Ipv4Address &address)
168 {
169  UdpFaceMap::iterator i = m_udpFaceMap.find (address);
170  if (i != m_udpFaceMap.end ())
171  return i->second;
172  else
173  return 0;
174 }
175 
176 Ptr<TcpFace>
177 IpFaceStack::CreateOrGetTcpFace (Ipv4Address address, Callback< void, Ptr<Face> > onCreate)
178 {
179  NS_LOG_FUNCTION (address);
180 
181  TcpFaceMap::iterator i = m_tcpFaceMap.find (address);
182  if (i != m_tcpFaceMap.end ())
183  return i->second;
184 
185  Ptr<Socket> socket = Socket::CreateSocket (m_node, TcpSocketFactory::GetTypeId ());
186  Ptr<TcpFace> face = CreateObject<TcpFace> (m_node, socket, address);
187 
188  face->SetCreateCallback (onCreate);
189 
190  socket->SetConnectCallback (MakeCallback (&TcpFace::OnConnect, face),
191  MakeNullCallback< void, Ptr< Socket > > ());
192  socket->Connect (InetSocketAddress (address, L3Protocol::IP_STACK_PORT));
193 
194  m_tcpFaceMap.insert (std::make_pair (address, face));
195 
196  return face;
197 }
198 
199 Ptr<UdpFace>
200 IpFaceStack::CreateOrGetUdpFace (Ipv4Address address)
201 {
202  NS_LOG_FUNCTION (address);
203 
204  UdpFaceMap::iterator i = m_udpFaceMap.find (address);
205  if (i != m_udpFaceMap.end ())
206  return i->second;
207 
208  Ptr<Socket> socket = Socket::CreateSocket (m_node, UdpSocketFactory::GetTypeId ());
209  socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), L3Protocol::IP_STACK_PORT)); // not sure if it going to work...
210  // socket->Bind ();
211  socket->Connect (InetSocketAddress (address, L3Protocol::IP_STACK_PORT));
212 
213  Ptr<UdpFace> face = CreateObject<UdpFace> (m_node, socket, address);
214  Ptr<L3Protocol> ndn = m_node->GetObject<L3Protocol> ();
215 
216  ndn->AddFace (face);
217  face->SetUp (true);
218 
219  m_udpFaceMap.insert (std::make_pair (address, face));
220  return face;
221 }
222 
223 
224 } // namespace ndn
225 } // namespace ns3
virtual uint32_t AddFace(const Ptr< Face > &face)
Add face to Ndn stack.
Ptr< UdpFace > GetUdpFaceByAddress(const Ipv4Address &addr)
Lookup UdpFace for a given address.
Implementation network-layer of NDN stack.
Ptr< TcpFace > GetTcpFaceByAddress(const Ipv4Address &addr)
Lookup TcpFace for a given address.
void DestroyTcpFace(Ptr< TcpFace > face)
Destroy TcpFace, e.g., after TCP connection got dropped.
static const uint16_t IP_STACK_PORT
TCP/UDP port for NDN stack.
Ptr< TcpFace > CreateOrGetTcpFace(Ipv4Address address, Callback< void, Ptr< Face > > onCreate=NULL_CREATE_CALLBACK)
Method allowing creation and lookup of faces.
IpFaceStack()
Default constructor.
Ptr< UdpFace > CreateOrGetUdpFace(Ipv4Address address)
Method allowing creation and lookup of faces.