|
1 |
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
2 |
/* |
3 |
* Copyright (c) 2010 Keio University |
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: Hajime Tazaki <tazaki@sfc.wide.ad.jp> |
19 |
*/ |
20 |
/** |
21 |
* This is the test code for ipv4-raw-socket-impl.cc. |
22 |
*/ |
23 |
|
24 |
#include "ns3/test.h" |
25 |
#include "ns3/socket-factory.h" |
26 |
#include "ns3/ipv4-raw-socket-factory.h" |
27 |
#include "ns3/simulator.h" |
28 |
#include "ns3/simple-channel.h" |
29 |
#include "ns3/simple-net-device.h" |
30 |
#include "ns3/drop-tail-queue.h" |
31 |
#include "ns3/socket.h" |
32 |
|
33 |
#include "ns3/log.h" |
34 |
#include "ns3/node.h" |
35 |
#include "ns3/inet-socket-address.h" |
36 |
|
37 |
#include "arp-l3-protocol.h" |
38 |
#include "ipv4-l3-protocol.h" |
39 |
#include "icmpv4-l4-protocol.h" |
40 |
#include "ns3/ipv4-list-routing.h" |
41 |
#include "ns3/ipv4-static-routing.h" |
42 |
|
43 |
#include <string> |
44 |
#include <limits> |
45 |
namespace ns3 { |
46 |
|
47 |
static void |
48 |
AddInternetStack (Ptr<Node> node) |
49 |
{ |
50 |
//ARP |
51 |
Ptr<ArpL3Protocol> arp = CreateObject<ArpL3Protocol> (); |
52 |
node->AggregateObject(arp); |
53 |
//IPV4 |
54 |
Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> (); |
55 |
//Routing for Ipv4 |
56 |
Ptr<Ipv4ListRouting> ipv4Routing = CreateObject<Ipv4ListRouting> (); |
57 |
ipv4->SetRoutingProtocol (ipv4Routing); |
58 |
Ptr<Ipv4StaticRouting> ipv4staticRouting = CreateObject<Ipv4StaticRouting> (); |
59 |
ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0); |
60 |
node->AggregateObject(ipv4); |
61 |
//ICMP |
62 |
Ptr<Icmpv4L4Protocol> icmp = CreateObject<Icmpv4L4Protocol> (); |
63 |
node->AggregateObject(icmp); |
64 |
// //Ipv4Raw |
65 |
// Ptr<Ipv4UdpL4Protocol> udp = CreateObject<UdpL4Protocol> (); |
66 |
// node->AggregateObject(udp); |
67 |
} |
68 |
|
69 |
|
70 |
class Ipv4RawSocketImplTest: public TestCase |
71 |
{ |
72 |
Ptr<Packet> m_receivedPacket; |
73 |
Ptr<Packet> m_receivedPacket2; |
74 |
void DoSendData (Ptr<Socket> socket, std::string to); |
75 |
void SendData (Ptr<Socket> socket, std::string to); |
76 |
|
77 |
public: |
78 |
virtual bool DoRun (void); |
79 |
Ipv4RawSocketImplTest (); |
80 |
|
81 |
void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from); |
82 |
void ReceivePacket2 (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from); |
83 |
void ReceivePkt (Ptr<Socket> socket); |
84 |
void ReceivePkt2 (Ptr<Socket> socket); |
85 |
}; |
86 |
|
87 |
|
88 |
Ipv4RawSocketImplTest::Ipv4RawSocketImplTest () |
89 |
: TestCase ("IPv4 Raw socket implementation") |
90 |
{ |
91 |
} |
92 |
|
93 |
void Ipv4RawSocketImplTest::ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from) |
94 |
{ |
95 |
m_receivedPacket = packet; |
96 |
} |
97 |
|
98 |
void Ipv4RawSocketImplTest::ReceivePacket2 (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from) |
99 |
{ |
100 |
m_receivedPacket2 = packet; |
101 |
} |
102 |
|
103 |
void Ipv4RawSocketImplTest::ReceivePkt (Ptr<Socket> socket) |
104 |
{ |
105 |
uint32_t availableData; |
106 |
availableData = socket->GetRxAvailable (); |
107 |
m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max(), 0); |
108 |
NS_ASSERT (availableData == m_receivedPacket->GetSize ()); |
109 |
} |
110 |
|
111 |
void Ipv4RawSocketImplTest::ReceivePkt2 (Ptr<Socket> socket) |
112 |
{ |
113 |
uint32_t availableData; |
114 |
availableData = socket->GetRxAvailable (); |
115 |
m_receivedPacket2 = socket->Recv (std::numeric_limits<uint32_t>::max(), 0); |
116 |
NS_ASSERT (availableData == m_receivedPacket2->GetSize ()); |
117 |
} |
118 |
|
119 |
void |
120 |
Ipv4RawSocketImplTest::DoSendData (Ptr<Socket> socket, std::string to) |
121 |
{ |
122 |
Address realTo = InetSocketAddress (Ipv4Address(to.c_str()), 0); |
123 |
NS_TEST_EXPECT_MSG_NE (socket->SendTo (Create<Packet> (123), 0, realTo), |
124 |
-1, to); |
125 |
} |
126 |
|
127 |
void |
128 |
Ipv4RawSocketImplTest::SendData (Ptr<Socket> socket, std::string to) |
129 |
{ |
130 |
m_receivedPacket = Create<Packet> (); |
131 |
m_receivedPacket2 = Create<Packet> (); |
132 |
Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0), |
133 |
&Ipv4RawSocketImplTest::DoSendData, this, socket, to); |
134 |
Simulator::Run (); |
135 |
} |
136 |
|
137 |
bool |
138 |
Ipv4RawSocketImplTest::DoRun (void) |
139 |
{ |
140 |
// Create topology |
141 |
|
142 |
// Receiver Node |
143 |
Ptr<Node> rxNode = CreateObject<Node> (); |
144 |
AddInternetStack (rxNode); |
145 |
Ptr<SimpleNetDevice> rxDev1, rxDev2; |
146 |
{ // first interface |
147 |
rxDev1 = CreateObject<SimpleNetDevice> (); |
148 |
rxDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ())); |
149 |
rxNode->AddDevice (rxDev1); |
150 |
Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> (); |
151 |
uint32_t netdev_idx = ipv4->AddInterface (rxDev1); |
152 |
Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.1"), Ipv4Mask (0xffff0000U)); |
153 |
ipv4->AddAddress (netdev_idx, ipv4Addr); |
154 |
ipv4->SetUp (netdev_idx); |
155 |
} |
156 |
|
157 |
{ // second interface |
158 |
rxDev2 = CreateObject<SimpleNetDevice> (); |
159 |
rxDev2->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ())); |
160 |
rxNode->AddDevice (rxDev2); |
161 |
Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> (); |
162 |
uint32_t netdev_idx = ipv4->AddInterface (rxDev2); |
163 |
Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.1.1"), Ipv4Mask (0xffff0000U)); |
164 |
ipv4->AddAddress (netdev_idx, ipv4Addr); |
165 |
ipv4->SetUp (netdev_idx); |
166 |
} |
167 |
|
168 |
// Sender Node |
169 |
Ptr<Node> txNode = CreateObject<Node> (); |
170 |
AddInternetStack (txNode); |
171 |
Ptr<SimpleNetDevice> txDev1; |
172 |
{ |
173 |
txDev1 = CreateObject<SimpleNetDevice> (); |
174 |
txDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ())); |
175 |
txNode->AddDevice (txDev1); |
176 |
Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> (); |
177 |
uint32_t netdev_idx = ipv4->AddInterface (txDev1); |
178 |
Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.2"), Ipv4Mask (0xffff0000U)); |
179 |
ipv4->AddAddress (netdev_idx, ipv4Addr); |
180 |
ipv4->SetUp (netdev_idx); |
181 |
} |
182 |
Ptr<SimpleNetDevice> txDev2; |
183 |
{ |
184 |
txDev2 = CreateObject<SimpleNetDevice> (); |
185 |
txDev2->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ())); |
186 |
txNode->AddDevice (txDev2); |
187 |
Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> (); |
188 |
uint32_t netdev_idx = ipv4->AddInterface (txDev2); |
189 |
Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.1.2"), Ipv4Mask (0xffff0000U)); |
190 |
ipv4->AddAddress (netdev_idx, ipv4Addr); |
191 |
ipv4->SetUp (netdev_idx); |
192 |
} |
193 |
|
194 |
// link the two nodes |
195 |
Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> (); |
196 |
rxDev1->SetChannel (channel1); |
197 |
txDev1->SetChannel (channel1); |
198 |
|
199 |
Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel> (); |
200 |
rxDev2->SetChannel (channel2); |
201 |
txDev2->SetChannel (channel2); |
202 |
|
203 |
|
204 |
// Create the IPv4 Raw sockets |
205 |
Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv4RawSocketFactory> (); |
206 |
Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket (); |
207 |
NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0.0"), 0)), 0, "trivial"); |
208 |
rxSocket->SetRecvCallback (MakeCallback (&Ipv4RawSocketImplTest::ReceivePkt, this)); |
209 |
|
210 |
Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket (); |
211 |
rxSocket2->SetRecvCallback (MakeCallback (&Ipv4RawSocketImplTest::ReceivePkt2, this)); |
212 |
NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (InetSocketAddress (Ipv4Address ("10.0.1.1"), 0)), 0, "trivial"); |
213 |
|
214 |
Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv4RawSocketFactory> (); |
215 |
Ptr<Socket> txSocket = txSocketFactory->CreateSocket (); |
216 |
|
217 |
// ------ Now the tests ------------ |
218 |
|
219 |
// Unicast test |
220 |
SendData (txSocket, "10.0.0.1"); |
221 |
NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 10.0.0.1"); |
222 |
NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0, "second interface should not receive it"); |
223 |
|
224 |
m_receivedPacket->RemoveAllByteTags (); |
225 |
m_receivedPacket2->RemoveAllByteTags (); |
226 |
|
227 |
#if 0 |
228 |
// Simple broadcast test |
229 |
|
230 |
SendData (txSocket, "255.255.255.255"); |
231 |
NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 255.255.255.255"); |
232 |
NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 143, "second socket should not receive it (it is bound specifically to the second interface's address"); |
233 |
|
234 |
m_receivedPacket->RemoveAllByteTags (); |
235 |
m_receivedPacket2->RemoveAllByteTags (); |
236 |
#endif |
237 |
|
238 |
// Simple Link-local multicast test |
239 |
|
240 |
txSocket->Bind (InetSocketAddress (Ipv4Address ("10.0.1.2"), 0)); |
241 |
SendData (txSocket, "224.0.0.9"); |
242 |
NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 224.0.0.9"); |
243 |
NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0, "second socket should not receive it (it is bound specifically to the second interface's address"); |
244 |
|
245 |
m_receivedPacket->RemoveAllByteTags (); |
246 |
m_receivedPacket2->RemoveAllByteTags (); |
247 |
|
248 |
#if 0 |
249 |
// Broadcast test with multiple receiving sockets |
250 |
|
251 |
// When receiving broadcast packets, all sockets sockets bound to |
252 |
// the address/port should receive a copy of the same packet -- if |
253 |
// the socket address matches. |
254 |
rxSocket2->Dispose (); |
255 |
rxSocket2 = rxSocketFactory->CreateSocket (); |
256 |
rxSocket2->SetRecvCallback (MakeCallback (&Ipv4RawSocketImplTest::ReceivePkt2, this)); |
257 |
NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0"), 0)), 0, "trivial"); |
258 |
|
259 |
SendData (txSocket, "255.255.255.255"); |
260 |
NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 255.255.255.255"); |
261 |
NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 143, "recv: 255.255.255.255"); |
262 |
#endif |
263 |
|
264 |
m_receivedPacket = 0; |
265 |
m_receivedPacket2 = 0; |
266 |
|
267 |
Simulator::Destroy (); |
268 |
|
269 |
return GetErrorStatus (); |
270 |
} |
271 |
//----------------------------------------------------------------------------- |
272 |
class Ipv4RawTestSuite : public TestSuite |
273 |
{ |
274 |
public: |
275 |
Ipv4RawTestSuite () : TestSuite ("ipv4-raw", UNIT) |
276 |
{ |
277 |
AddTestCase (new Ipv4RawSocketImplTest); |
278 |
} |
279 |
} g_ipv4rawTestSuite; |
280 |
|
281 |
}; // namespace ns3 |