OLD | NEW |
(Empty) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2010 Hajime Tazaki |
| 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 * Authors: Hajime Tazaki <tazaki@sfc.wide.ad.jp> |
| 19 */ |
| 20 |
| 21 |
| 22 #include <stdint.h> |
| 23 #include "ns3/ipv4-address.h" |
| 24 #include "ns3/test.h" |
| 25 #include "ipv4-packet-info-tag.h" |
| 26 |
| 27 namespace ns3 { |
| 28 |
| 29 Ipv4PacketInfoTag::Ipv4PacketInfoTag ()·· |
| 30 : m_addr (Ipv4Address ()), |
| 31 m_spec_dst (Ipv4Address ()), |
| 32 m_ifindex (0), |
| 33 m_ttl (0) |
| 34 { |
| 35 } |
| 36 |
| 37 void· |
| 38 Ipv4PacketInfoTag::SetAddress (Ipv4Address addr) |
| 39 { |
| 40 m_addr = addr; |
| 41 } |
| 42 |
| 43 Ipv4Address |
| 44 Ipv4PacketInfoTag::GetAddress (void) const |
| 45 { |
| 46 return m_addr; |
| 47 } |
| 48 |
| 49 void· |
| 50 Ipv4PacketInfoTag::SetLocalAddress (Ipv4Address addr) |
| 51 { |
| 52 m_spec_dst = addr; |
| 53 } |
| 54 |
| 55 Ipv4Address |
| 56 Ipv4PacketInfoTag::GetLocalAddress (void) const |
| 57 { |
| 58 return m_spec_dst; |
| 59 } |
| 60 |
| 61 void· |
| 62 Ipv4PacketInfoTag::SetRecvIf (uint32_t ifindex) |
| 63 { |
| 64 m_ifindex = ifindex; |
| 65 } |
| 66 |
| 67 uint32_t· |
| 68 Ipv4PacketInfoTag::GetRecvIf (void) const |
| 69 { |
| 70 return m_ifindex; |
| 71 } |
| 72 |
| 73 void· |
| 74 Ipv4PacketInfoTag::SetTtl (uint8_t ttl) |
| 75 { |
| 76 m_ttl = ttl; |
| 77 } |
| 78 |
| 79 uint8_t· |
| 80 Ipv4PacketInfoTag::GetTtl (void) const |
| 81 { |
| 82 return m_ttl; |
| 83 } |
| 84 |
| 85 |
| 86 |
| 87 TypeId |
| 88 Ipv4PacketInfoTag::GetTypeId (void) |
| 89 { |
| 90 static TypeId tid = TypeId ("ns3::Ipv4PacketInfoTag") |
| 91 .SetParent<Tag> () |
| 92 .AddConstructor<Ipv4PacketInfoTag> () |
| 93 ; |
| 94 return tid; |
| 95 } |
| 96 TypeId |
| 97 Ipv4PacketInfoTag::GetInstanceTypeId (void) const |
| 98 { |
| 99 return GetTypeId (); |
| 100 } |
| 101 |
| 102 uint32_t· |
| 103 Ipv4PacketInfoTag::GetSerializedSize (void) const |
| 104 {· |
| 105 return 4 + 4· |
| 106 + sizeof (uint32_t) |
| 107 + sizeof (uint8_t); |
| 108 } |
| 109 void· |
| 110 Ipv4PacketInfoTag::Serialize (TagBuffer i) const |
| 111 {· |
| 112 uint8_t buf[4]; |
| 113 m_addr.Serialize (buf); |
| 114 i.Write (buf, 4); |
| 115 m_spec_dst.Serialize (buf); |
| 116 i.Write (buf, 4); |
| 117 i.WriteU32 (m_ifindex); |
| 118 i.WriteU8 (m_ttl); |
| 119 } |
| 120 void· |
| 121 Ipv4PacketInfoTag::Deserialize (TagBuffer i) |
| 122 {· |
| 123 uint8_t buf[4]; |
| 124 i.Read (buf, 4); |
| 125 m_addr.Deserialize (buf); |
| 126 i.Read (buf, 4); |
| 127 m_spec_dst.Deserialize (buf); |
| 128 m_ifindex = i.ReadU32 (); |
| 129 m_ttl = i.ReadU8 (); |
| 130 } |
| 131 void |
| 132 Ipv4PacketInfoTag::Print (std::ostream &os) const |
| 133 { |
| 134 os << "Ipv4 PKTINFO [DestAddr: " << m_addr; |
| 135 os << ", Local Address:" << m_spec_dst; |
| 136 os << ", RecvIf:" << (uint32_t) m_ifindex; |
| 137 os << ", TTL:" << (uint32_t) m_ttl; |
| 138 os << "] "; |
| 139 } |
| 140 }//namespace ns3 |
| 141 |
| 142 //----------------------------------------------------------------------------- |
| 143 // Unit tests |
| 144 //----------------------------------------------------------------------------- |
| 145 |
| 146 #include "ns3/log.h" |
| 147 #include "ns3/abort.h" |
| 148 #include "ns3/attribute.h" |
| 149 #include "ns3/object-factory.h" |
| 150 #include "ns3/internet-stack-helper.h" |
| 151 #include "ns3/ipv4-address-helper.h" |
| 152 #include "ns3/csma-helper.h" |
| 153 #include "ns3/node-container.h" |
| 154 #include "ns3/object-factory.h" |
| 155 #include "ns3/socket-factory.h" |
| 156 #include "ns3/udp-socket-factory.h" |
| 157 #include "ns3/udp-socket.h" |
| 158 #include "ns3/ipv4-raw-socket-factory.h" |
| 159 #include "ns3/inet-socket-address.h" |
| 160 #include "ns3/simulator.h" |
| 161 |
| 162 namespace ns3 { |
| 163 |
| 164 class Ipv4PacketInfoTagTest: public TestCase· |
| 165 { |
| 166 public: |
| 167 Ipv4PacketInfoTagTest (); |
| 168 private: |
| 169 virtual bool DoRun (void); |
| 170 void RxCb (Ptr<Socket> socket); |
| 171 void DoSendData (Ptr<Socket> socket, std::string to); |
| 172 }; |
| 173 |
| 174 Ipv4PacketInfoTagTest::Ipv4PacketInfoTagTest () |
| 175 : TestCase ("Ipv4PacketInfoTagTest")· |
| 176 {} |
| 177 |
| 178 void |
| 179 Ipv4PacketInfoTagTest::RxCb (Ptr<Socket> socket) |
| 180 { |
| 181 uint32_t availableData; |
| 182 Ptr<Packet> m_receivedPacket; |
| 183 |
| 184 availableData = socket->GetRxAvailable (); |
| 185 m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max(), 0); |
| 186 NS_ASSERT (availableData == m_receivedPacket->GetSize ()); |
| 187 |
| 188 Ipv4PacketInfoTag tag; |
| 189 bool found; |
| 190 found = m_receivedPacket->RemovePacketTag (tag); |
| 191 NS_ASSERT (found); |
| 192 tag.Print (std::cout); |
| 193 } |
| 194 |
| 195 void |
| 196 Ipv4PacketInfoTagTest::DoSendData (Ptr<Socket> socket, std::string to) |
| 197 { |
| 198 Address realTo = InetSocketAddress (Ipv4Address (to.c_str()), 200); |
| 199 if (DynamicCast<UdpSocket> (socket) != 0) |
| 200 { |
| 201 NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo), |
| 202 123, "XXX"); |
| 203 } |
| 204 // Should only Ipv4RawSock |
| 205 else |
| 206 { |
| 207 socket->SendTo (Create<Packet> (123), 0, realTo); |
| 208 } |
| 209 } |
| 210 |
| 211 bool |
| 212 Ipv4PacketInfoTagTest::DoRun (void) |
| 213 { |
| 214 NodeContainer n; |
| 215 n.Create (2); |
| 216 |
| 217 InternetStackHelper internet; |
| 218 internet.Install (n); |
| 219 |
| 220 CsmaHelper csma; |
| 221 NetDeviceContainer d = csma.Install (n); |
| 222 |
| 223 Ipv4AddressHelper ipv4; |
| 224 ipv4.SetBase ("10.1.1.0", "255.255.255.0"); |
| 225 Ipv4InterfaceContainer i = ipv4.Assign (d); |
| 226 |
| 227 // IPv4 test |
| 228 Ptr<SocketFactory> factory = n.Get (0)->GetObject<SocketFactory> (UdpSocketFac
tory::GetTypeId ()); |
| 229 Ptr<Socket> socket = factory->CreateSocket (); |
| 230 InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 200); |
| 231 socket->Bind (local); |
| 232 socket->SetRecvPktInfo (true); |
| 233 socket->SetRecvCallback (MakeCallback (&ns3::Ipv4PacketInfoTagTest::RxCb, this
)); |
| 234 |
| 235 // receive on loopback· |
| 236 Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0), |
| 237 &Ipv4PacketInfoTagTest::DoSendData, this, sock
et, "127.0.0.1"); |
| 238 Simulator::Run (); |
| 239 |
| 240 // send from node1 and recved via csma |
| 241 Ptr<SocketFactory> factory2 = n.Get (1)->GetObject<SocketFactory> (UdpSocketFa
ctory::GetTypeId ()); |
| 242 Ptr<Socket> socket2 = factory2->CreateSocket (); |
| 243 Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0), |
| 244 &Ipv4PacketInfoTagTest::DoSendData, this, sock
et, "10.1.1.1"); |
| 245 Simulator::Run (); |
| 246 |
| 247 // ipv4 w rawsocket |
| 248 factory = n.Get (0)->GetObject<SocketFactory> (Ipv4RawSocketFactory::GetTypeId
()); |
| 249 socket = factory->CreateSocket (); |
| 250 local = InetSocketAddress (Ipv4Address::GetAny (), 0); |
| 251 socket->Bind (local); |
| 252 socket->SetRecvPktInfo (true); |
| 253 socket->SetRecvCallback (MakeCallback (&ns3::Ipv4PacketInfoTagTest::RxCb, this
)); |
| 254 |
| 255 // receive on loopback· |
| 256 Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0), |
| 257 &Ipv4PacketInfoTagTest::DoSendData, this, sock
et, "127.0.0.1"); |
| 258 Simulator::Run (); |
| 259 |
| 260 // send from node1 and recved via csma |
| 261 factory2 = n.Get (1)->GetObject<SocketFactory> (Ipv4RawSocketFactory::GetTypeI
d ()); |
| 262 socket2 = factory2->CreateSocket (); |
| 263 Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0), |
| 264 &Ipv4PacketInfoTagTest::DoSendData, this, sock
et, "10.1.1.1"); |
| 265 Simulator::Run (); |
| 266 |
| 267 return 0; |
| 268 } |
| 269 |
| 270 static class Ipv4PacketInfoTagTestSuite : public TestSuite |
| 271 { |
| 272 public: |
| 273 Ipv4PacketInfoTagTestSuite (); |
| 274 private: |
| 275 } g_packetinfotagTests; |
| 276 |
| 277 Ipv4PacketInfoTagTestSuite::Ipv4PacketInfoTagTestSuite () |
| 278 : TestSuite ("ipv4-packet-info-tag", UNIT) |
| 279 { |
| 280 AddTestCase (new Ipv4PacketInfoTagTest ()); |
| 281 } |
| 282 |
| 283 |
| 284 } |
OLD | NEW |