00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "ns3/log.h"
00022 #include "ns3/node.h"
00023 #include "ns3/inet-socket-address.h"
00024 #include "ns3/ipv4-route.h"
00025 #include "ns3/ipv4.h"
00026 #include "ns3/udp-socket-factory.h"
00027 #include "ns3/trace-source-accessor.h"
00028 #include "udp-socket-impl.h"
00029 #include "udp-l4-protocol.h"
00030 #include "ipv4-end-point.h"
00031 #include <limits>
00032
00033 NS_LOG_COMPONENT_DEFINE ("UdpSocketImpl");
00034
00035 namespace ns3 {
00036
00037 static const uint32_t MAX_IPV4_UDP_DATAGRAM_SIZE = 65507;
00038
00039
00040 TypeId
00041 UdpSocketImpl::GetTypeId (void)
00042 {
00043 static TypeId tid = TypeId ("ns3::UdpSocketImpl")
00044 .SetParent<UdpSocket> ()
00045 .AddConstructor<UdpSocketImpl> ()
00046 .AddTraceSource ("Drop", "Drop UDP packet due to receive buffer overflow",
00047 MakeTraceSourceAccessor (&UdpSocketImpl::m_dropTrace))
00048 .AddAttribute ("IcmpCallback", "Callback invoked whenever an icmp error is received on this socket.",
00049 CallbackValue (),
00050 MakeCallbackAccessor (&UdpSocketImpl::m_icmpCallback),
00051 MakeCallbackChecker ())
00052 ;
00053 return tid;
00054 }
00055
00056 UdpSocketImpl::UdpSocketImpl ()
00057 : m_endPoint (0),
00058 m_node (0),
00059 m_udp (0),
00060 m_errno (ERROR_NOTERROR),
00061 m_shutdownSend (false),
00062 m_shutdownRecv (false),
00063 m_connected (false),
00064 m_rxAvailable (0)
00065 {
00066 NS_LOG_FUNCTION_NOARGS ();
00067 }
00068
00069 UdpSocketImpl::~UdpSocketImpl ()
00070 {
00071 NS_LOG_FUNCTION_NOARGS ();
00072
00073 m_node = 0;
00074 if (m_endPoint != 0)
00075 {
00076 NS_ASSERT (m_udp != 0);
00077
00078
00079
00080
00081
00082
00083
00084
00085 NS_ASSERT (m_endPoint != 0);
00086 m_udp->DeAllocate (m_endPoint);
00087 NS_ASSERT (m_endPoint == 0);
00088 }
00089 m_udp = 0;
00090 }
00091
00092 void
00093 UdpSocketImpl::SetNode (Ptr<Node> node)
00094 {
00095 NS_LOG_FUNCTION_NOARGS ();
00096 m_node = node;
00097
00098 }
00099 void
00100 UdpSocketImpl::SetUdp (Ptr<UdpL4Protocol> udp)
00101 {
00102 NS_LOG_FUNCTION_NOARGS ();
00103 m_udp = udp;
00104 }
00105
00106
00107 enum Socket::SocketErrno
00108 UdpSocketImpl::GetErrno (void) const
00109 {
00110 NS_LOG_FUNCTION_NOARGS ();
00111 return m_errno;
00112 }
00113
00114 Ptr<Node>
00115 UdpSocketImpl::GetNode (void) const
00116 {
00117 NS_LOG_FUNCTION_NOARGS ();
00118 return m_node;
00119 }
00120
00121 void
00122 UdpSocketImpl::Destroy (void)
00123 {
00124 NS_LOG_FUNCTION_NOARGS ();
00125 m_node = 0;
00126 m_endPoint = 0;
00127 m_udp = 0;
00128 }
00129
00130 int
00131 UdpSocketImpl::FinishBind (void)
00132 {
00133 NS_LOG_FUNCTION_NOARGS ();
00134 if (m_endPoint == 0)
00135 {
00136 return -1;
00137 }
00138 m_endPoint->SetRxCallback (MakeCallback (&UdpSocketImpl::ForwardUp, Ptr<UdpSocketImpl> (this)));
00139 m_endPoint->SetIcmpCallback (MakeCallback (&UdpSocketImpl::ForwardIcmp, Ptr<UdpSocketImpl> (this)));
00140 m_endPoint->SetDestroyCallback (MakeCallback (&UdpSocketImpl::Destroy, Ptr<UdpSocketImpl> (this)));
00141 return 0;
00142 }
00143
00144 int
00145 UdpSocketImpl::Bind (void)
00146 {
00147 NS_LOG_FUNCTION_NOARGS ();
00148 m_endPoint = m_udp->Allocate ();
00149 return FinishBind ();
00150 }
00151
00152 int
00153 UdpSocketImpl::Bind (const Address &address)
00154 {
00155 NS_LOG_FUNCTION (this << address);
00156
00157 if (!InetSocketAddress::IsMatchingType (address))
00158 {
00159 NS_LOG_ERROR ("Not IsMatchingType");
00160 m_errno = ERROR_INVAL;
00161 return -1;
00162 }
00163 InetSocketAddress transport = InetSocketAddress::ConvertFrom (address);
00164 Ipv4Address ipv4 = transport.GetIpv4 ();
00165 uint16_t port = transport.GetPort ();
00166 if (ipv4 == Ipv4Address::GetAny () && port == 0)
00167 {
00168 m_endPoint = m_udp->Allocate ();
00169 }
00170 else if (ipv4 == Ipv4Address::GetAny () && port != 0)
00171 {
00172 m_endPoint = m_udp->Allocate (port);
00173 }
00174 else if (ipv4 != Ipv4Address::GetAny () && port == 0)
00175 {
00176 m_endPoint = m_udp->Allocate (ipv4);
00177 }
00178 else if (ipv4 != Ipv4Address::GetAny () && port != 0)
00179 {
00180 m_endPoint = m_udp->Allocate (ipv4, port);
00181 }
00182
00183 return FinishBind ();
00184 }
00185
00186 int
00187 UdpSocketImpl::ShutdownSend (void)
00188 {
00189 NS_LOG_FUNCTION_NOARGS ();
00190 m_shutdownSend = true;
00191 return 0;
00192 }
00193
00194 int
00195 UdpSocketImpl::ShutdownRecv (void)
00196 {
00197 NS_LOG_FUNCTION_NOARGS ();
00198 m_shutdownRecv = false;
00199 return 0;
00200 }
00201
00202 int
00203 UdpSocketImpl::Close(void)
00204 {
00205 NS_LOG_FUNCTION_NOARGS ();
00206 return 0;
00207 }
00208
00209 int
00210 UdpSocketImpl::Connect(const Address & address)
00211 {
00212 NS_LOG_FUNCTION (this << address);
00213 InetSocketAddress transport = InetSocketAddress::ConvertFrom (address);
00214 m_defaultAddress = transport.GetIpv4 ();
00215 m_defaultPort = transport.GetPort ();
00216 NotifyConnectionSucceeded ();
00217 m_connected = true;
00218
00219 return 0;
00220 }
00221
00222 int
00223 UdpSocketImpl::Listen (void)
00224 {
00225 m_errno = Socket::ERROR_OPNOTSUPP;
00226 return -1;
00227 }
00228
00229 int
00230 UdpSocketImpl::Send (Ptr<Packet> p, uint32_t flags)
00231 {
00232 NS_LOG_FUNCTION (this << p << flags);
00233
00234 if (!m_connected)
00235 {
00236 m_errno = ERROR_NOTCONN;
00237 return -1;
00238 }
00239 return DoSend (p);
00240 }
00241
00242 int
00243 UdpSocketImpl::DoSend (Ptr<Packet> p)
00244 {
00245 NS_LOG_FUNCTION (this << p);
00246 if (m_endPoint == 0)
00247 {
00248 if (Bind () == -1)
00249 {
00250 NS_ASSERT (m_endPoint == 0);
00251 return -1;
00252 }
00253 NS_ASSERT (m_endPoint != 0);
00254 }
00255 if (m_shutdownSend)
00256 {
00257 m_errno = ERROR_SHUTDOWN;
00258 return -1;
00259 }
00260
00261 return DoSendTo (p, m_defaultAddress, m_defaultPort);
00262 }
00263
00264 int
00265 UdpSocketImpl::DoSendTo (Ptr<Packet> p, const Address &address)
00266 {
00267 NS_LOG_FUNCTION (this << p << address);
00268
00269 if (!m_connected)
00270 {
00271 NS_LOG_LOGIC ("Not connected");
00272 InetSocketAddress transport = InetSocketAddress::ConvertFrom (address);
00273 Ipv4Address ipv4 = transport.GetIpv4 ();
00274 uint16_t port = transport.GetPort ();
00275 return DoSendTo (p, ipv4, port);
00276 }
00277 else
00278 {
00279
00280 NS_LOG_LOGIC ("Connected");
00281 return DoSendTo (p, m_defaultAddress, m_defaultPort);
00282 }
00283 }
00284
00285 int
00286 UdpSocketImpl::DoSendTo (Ptr<Packet> p, Ipv4Address dest, uint16_t port)
00287 {
00288 NS_LOG_FUNCTION (this << p << dest << port);
00289
00290 if (m_endPoint == 0)
00291 {
00292 if (Bind () == -1)
00293 {
00294 NS_ASSERT (m_endPoint == 0);
00295 return -1;
00296 }
00297 NS_ASSERT (m_endPoint != 0);
00298 }
00299 if (m_shutdownSend)
00300 {
00301 m_errno = ERROR_SHUTDOWN;
00302 return -1;
00303 }
00304
00305 if (p->GetSize () > GetTxAvailable () )
00306 {
00307 m_errno = ERROR_MSGSIZE;
00308 return -1;
00309 }
00310
00311 uint32_t localIfIndex;
00312 Ptr<Ipv4> ipv4 = m_node->GetObject<Ipv4> ();
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 if (m_ipMulticastTtl != 0 && dest.IsMulticast ())
00324 {
00325 SocketIpTtlTag tag;
00326 tag.SetTtl (m_ipMulticastTtl);
00327 p->AddTag (tag);
00328 }
00329 else if (m_ipTtl != 0 && !dest.IsMulticast () && !dest.IsBroadcast ())
00330 {
00331 SocketIpTtlTag tag;
00332 tag.SetTtl (m_ipTtl);
00333 p->AddTag (tag);
00334 }
00335 {
00336 SocketSetDontFragmentTag tag;
00337 bool found = p->FindFirstMatchingTag (tag);
00338 if (!found)
00339 {
00340 if (m_mtuDiscover)
00341 {
00342 tag.Enable ();
00343 }
00344 else
00345 {
00346 tag.Disable ();
00347 }
00348 p->AddTag (tag);
00349 }
00350 }
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360 if (dest.IsBroadcast ())
00361 {
00362 NS_LOG_LOGIC ("Limited broadcast start.");
00363 for (uint32_t i = 0; i < ipv4->GetNInterfaces (); i++ )
00364 {
00365 Ipv4Address addri = ipv4->GetAddress (i);
00366 Ipv4Mask maski = ipv4->GetNetworkMask (i);
00367 if (maski == Ipv4Mask::GetOnes ())
00368 {
00369
00370 NS_LOG_LOGIC ("Sending one copy from " << addri << " to " << dest
00371 << " (mask is " << maski << ")");
00372 m_udp->Send (p->Copy (), addri, dest,
00373 m_endPoint->GetLocalPort (), port);
00374 NotifyDataSent (p->GetSize ());
00375 NotifySend (GetTxAvailable ());
00376 }
00377 else
00378 {
00379
00380 Ipv4Address bcast = addri.GetSubnetDirectedBroadcast (maski);
00381 NS_LOG_LOGIC ("Sending one copy from " << addri << " to " << bcast
00382 << " (mask is " << maski << ")");
00383 m_udp->Send (p->Copy (), addri, bcast,
00384 m_endPoint->GetLocalPort (), port);
00385 NotifyDataSent (p->GetSize ());
00386 NotifySend (GetTxAvailable ());
00387 }
00388 }
00389 NS_LOG_LOGIC ("Limited broadcast end.");
00390 return p->GetSize();
00391 }
00392 else if (ipv4->GetIfIndexForDestination(dest, localIfIndex))
00393 {
00394 NS_LOG_LOGIC ("Route exists");
00395 m_udp->Send (p->Copy (), ipv4->GetAddress (localIfIndex), dest,
00396 m_endPoint->GetLocalPort (), port);
00397 NotifyDataSent (p->GetSize ());
00398 NotifySend (GetTxAvailable ());
00399 return p->GetSize();;
00400 }
00401 else
00402 {
00403 NS_LOG_ERROR ("ERROR_NOROUTETOHOST");
00404 m_errno = ERROR_NOROUTETOHOST;
00405 return -1;
00406 }
00407
00408 return 0;
00409 }
00410
00411
00412
00413 uint32_t
00414 UdpSocketImpl::GetTxAvailable (void) const
00415 {
00416 NS_LOG_FUNCTION_NOARGS ();
00417
00418
00419 return MAX_IPV4_UDP_DATAGRAM_SIZE;
00420 }
00421
00422 int
00423 UdpSocketImpl::SendTo (Ptr<Packet> p, uint32_t flags, const Address &address)
00424 {
00425 NS_LOG_FUNCTION (this << p << flags << address);
00426 InetSocketAddress transport = InetSocketAddress::ConvertFrom (address);
00427 Ipv4Address ipv4 = transport.GetIpv4 ();
00428 uint16_t port = transport.GetPort ();
00429 return DoSendTo (p, ipv4, port);
00430 }
00431
00432 uint32_t
00433 UdpSocketImpl::GetRxAvailable (void) const
00434 {
00435 NS_LOG_FUNCTION_NOARGS ();
00436
00437
00438 return m_rxAvailable;
00439 }
00440
00441 Ptr<Packet>
00442 UdpSocketImpl::Recv (uint32_t maxSize, uint32_t flags)
00443 {
00444 NS_LOG_FUNCTION (this << maxSize << flags);
00445 if (m_deliveryQueue.empty() )
00446 {
00447 m_errno = ERROR_AGAIN;
00448 return 0;
00449 }
00450 Ptr<Packet> p = m_deliveryQueue.front ();
00451 if (p->GetSize () <= maxSize)
00452 {
00453 m_deliveryQueue.pop ();
00454 m_rxAvailable -= p->GetSize ();
00455 }
00456 else
00457 {
00458 p = 0;
00459 }
00460 return p;
00461 }
00462
00463 Ptr<Packet>
00464 UdpSocketImpl::RecvFrom (uint32_t maxSize, uint32_t flags,
00465 Address &fromAddress)
00466 {
00467 NS_LOG_FUNCTION (this << maxSize << flags);
00468 Ptr<Packet> packet = Recv (maxSize, flags);
00469 if (packet != 0)
00470 {
00471 SocketAddressTag tag;
00472 bool found;
00473 found = packet->FindFirstMatchingTag (tag);
00474 NS_ASSERT (found);
00475 fromAddress = tag.GetAddress ();
00476 }
00477 return packet;
00478 }
00479
00480 int
00481 UdpSocketImpl::GetSockName (Address &address) const
00482 {
00483 NS_LOG_FUNCTION_NOARGS ();
00484 if (m_endPoint != 0)
00485 {
00486 address = InetSocketAddress (m_endPoint->GetLocalAddress (), m_endPoint->GetLocalPort());
00487 }
00488 else
00489 {
00490 address = InetSocketAddress(Ipv4Address::GetZero(), 0);
00491 }
00492 return 0;
00493 }
00494
00495 void
00496 UdpSocketImpl::ForwardUp (Ptr<Packet> packet, Ipv4Address ipv4, uint16_t port)
00497 {
00498 NS_LOG_FUNCTION (this << packet << ipv4 << port);
00499
00500 if (m_shutdownRecv)
00501 {
00502 return;
00503 }
00504 if ((m_rxAvailable + packet->GetSize ()) <= m_rcvBufSize)
00505 {
00506 Address address = InetSocketAddress (ipv4, port);
00507 SocketAddressTag tag;
00508 tag.SetAddress (address);
00509 packet->AddTag (tag);
00510 m_deliveryQueue.push (packet);
00511 m_rxAvailable += packet->GetSize ();
00512 NotifyDataRecv ();
00513 }
00514 else
00515 {
00516
00517
00518
00519
00520
00521 NS_LOG_WARN ("No receive buffer space available. Drop.");
00522 m_dropTrace (packet);
00523 }
00524 }
00525
00526 void
00527 UdpSocketImpl::ForwardIcmp (Ipv4Address icmpSource, uint8_t icmpTtl,
00528 uint8_t icmpType, uint8_t icmpCode,
00529 uint32_t icmpInfo)
00530 {
00531 NS_LOG_FUNCTION (this << icmpSource << (uint32_t)icmpTtl << (uint32_t)icmpType <<
00532 (uint32_t)icmpCode << icmpInfo);
00533 if (!m_icmpCallback.IsNull ())
00534 {
00535 m_icmpCallback (icmpSource, icmpTtl, icmpType, icmpCode, icmpInfo);
00536 }
00537 }
00538
00539
00540 void
00541 UdpSocketImpl::SetRcvBufSize (uint32_t size)
00542 {
00543 m_rcvBufSize = size;
00544 }
00545
00546 uint32_t
00547 UdpSocketImpl::GetRcvBufSize (void) const
00548 {
00549 return m_rcvBufSize;
00550 }
00551
00552 void
00553 UdpSocketImpl::SetIpTtl (uint32_t ipTtl)
00554 {
00555 m_ipTtl = ipTtl;
00556 }
00557
00558 uint32_t
00559 UdpSocketImpl::GetIpTtl (void) const
00560 {
00561 return m_ipTtl;
00562 }
00563
00564 void
00565 UdpSocketImpl::SetIpMulticastTtl (uint32_t ipTtl)
00566 {
00567 m_ipMulticastTtl = ipTtl;
00568 }
00569
00570 uint32_t
00571 UdpSocketImpl::GetIpMulticastTtl (void) const
00572 {
00573 return m_ipMulticastTtl;
00574 }
00575
00576 void
00577 UdpSocketImpl::SetMtuDiscover (bool discover)
00578 {
00579 m_mtuDiscover = discover;
00580 }
00581 bool
00582 UdpSocketImpl::GetMtuDiscover (void) const
00583 {
00584 return m_mtuDiscover;
00585 }
00586
00587
00588 }
00589
00590
00591 #ifdef RUN_SELF_TESTS
00592
00593 #include "ns3/test.h"
00594 #include "ns3/socket-factory.h"
00595 #include "ns3/udp-socket-factory.h"
00596 #include "ns3/simulator.h"
00597 #include "ns3/simple-channel.h"
00598 #include "ns3/simple-net-device.h"
00599 #include "ns3/drop-tail-queue.h"
00600 #include "internet-stack.h"
00601 #include <string>
00602
00603 namespace ns3 {
00604
00605 class UdpSocketImplTest: public Test
00606 {
00607 Ptr<Packet> m_receivedPacket;
00608 Ptr<Packet> m_receivedPacket2;
00609
00610 public:
00611 virtual bool RunTests (void);
00612 UdpSocketImplTest ();
00613
00614 void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
00615 void ReceivePacket2 (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
00616 void ReceivePkt (Ptr<Socket> socket);
00617 void ReceivePkt2 (Ptr<Socket> socket);
00618 };
00619
00620
00621 UdpSocketImplTest::UdpSocketImplTest ()
00622 : Test ("UdpSocketImpl")
00623 {
00624 }
00625
00626 void UdpSocketImplTest::ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from)
00627 {
00628 m_receivedPacket = packet;
00629 }
00630
00631 void UdpSocketImplTest::ReceivePacket2 (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from)
00632 {
00633 m_receivedPacket2 = packet;
00634 }
00635
00636 void UdpSocketImplTest::ReceivePkt (Ptr<Socket> socket)
00637 {
00638 uint32_t availableData;
00639 availableData = socket->GetRxAvailable ();
00640 m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max(), 0);
00641 NS_ASSERT (availableData == m_receivedPacket->GetSize ());
00642 }
00643
00644 void UdpSocketImplTest::ReceivePkt2 (Ptr<Socket> socket)
00645 {
00646 uint32_t availableData;
00647 availableData = socket->GetRxAvailable ();
00648 m_receivedPacket2 = socket->Recv (std::numeric_limits<uint32_t>::max(), 0);
00649 NS_ASSERT (availableData == m_receivedPacket2->GetSize ());
00650 }
00651
00652 bool
00653 UdpSocketImplTest::RunTests (void)
00654 {
00655 bool result = true;
00656
00657
00658
00659
00660 Ptr<Node> rxNode = CreateObject<Node> ();
00661 AddInternetStack (rxNode);
00662 Ptr<SimpleNetDevice> rxDev1, rxDev2;
00663 {
00664 rxDev1 = CreateObject<SimpleNetDevice> ();
00665 rxDev1->SetAddress (Mac48Address::Allocate ());
00666 rxNode->AddDevice (rxDev1);
00667 Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
00668 uint32_t netdev_idx = ipv4->AddInterface (rxDev1);
00669 ipv4->SetAddress (netdev_idx, Ipv4Address ("10.0.0.1"));
00670 ipv4->SetNetworkMask (netdev_idx, Ipv4Mask (0xffff0000U));
00671 ipv4->SetUp (netdev_idx);
00672 }
00673
00674 {
00675 rxDev2 = CreateObject<SimpleNetDevice> ();
00676 rxDev2->SetAddress (Mac48Address::Allocate ());
00677 rxNode->AddDevice (rxDev2);
00678 Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
00679 uint32_t netdev_idx = ipv4->AddInterface (rxDev2);
00680 ipv4->SetAddress (netdev_idx, Ipv4Address ("10.0.1.1"));
00681 ipv4->SetNetworkMask (netdev_idx, Ipv4Mask (0xffff0000U));
00682 ipv4->SetUp (netdev_idx);
00683 }
00684
00685
00686 Ptr<Node> txNode = CreateObject<Node> ();
00687 AddInternetStack (txNode);
00688 Ptr<SimpleNetDevice> txDev1;
00689 {
00690 txDev1 = CreateObject<SimpleNetDevice> ();
00691 txDev1->SetAddress (Mac48Address::Allocate ());
00692 txNode->AddDevice (txDev1);
00693 Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> ();
00694 uint32_t netdev_idx = ipv4->AddInterface (txDev1);
00695 ipv4->SetAddress (netdev_idx, Ipv4Address ("10.0.0.2"));
00696 ipv4->SetNetworkMask (netdev_idx, Ipv4Mask (0xffff0000U));
00697 ipv4->SetUp (netdev_idx);
00698 }
00699 Ptr<SimpleNetDevice> txDev2;
00700 {
00701 txDev2 = CreateObject<SimpleNetDevice> ();
00702 txDev2->SetAddress (Mac48Address::Allocate ());
00703 txNode->AddDevice (txDev2);
00704 Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> ();
00705 uint32_t netdev_idx = ipv4->AddInterface (txDev2);
00706 ipv4->SetAddress (netdev_idx, Ipv4Address ("10.0.1.2"));
00707 ipv4->SetNetworkMask (netdev_idx, Ipv4Mask (0xffff0000U));
00708 ipv4->SetUp (netdev_idx);
00709 }
00710
00711
00712 Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
00713 rxDev1->SetChannel (channel1);
00714 txDev1->SetChannel (channel1);
00715
00716 Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel> ();
00717 rxDev2->SetChannel (channel2);
00718 txDev2->SetChannel (channel2);
00719
00720
00721
00722 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory> ();
00723 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
00724 NS_TEST_ASSERT_EQUAL (rxSocket->Bind (InetSocketAddress (Ipv4Address ("10.0.0.1"), 1234)), 0);
00725 rxSocket->SetRecvCallback (MakeCallback (&UdpSocketImplTest::ReceivePkt, this));
00726
00727 Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket ();
00728 rxSocket2->SetRecvCallback (MakeCallback (&UdpSocketImplTest::ReceivePkt2, this));
00729 NS_TEST_ASSERT_EQUAL (rxSocket2->Bind (InetSocketAddress (Ipv4Address ("10.0.1.1"), 1234)), 0);
00730
00731 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory> ();
00732 Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
00733
00734
00735
00736
00737 m_receivedPacket = Create<Packet> ();
00738 m_receivedPacket2 = Create<Packet> ();
00739 NS_TEST_ASSERT_EQUAL (txSocket->SendTo ( Create<Packet> (123), 0,
00740 InetSocketAddress (Ipv4Address("10.0.0.1"), 1234)), 123);
00741 Simulator::Run ();
00742 NS_TEST_ASSERT_EQUAL (m_receivedPacket->GetSize (), 123);
00743 NS_TEST_ASSERT_EQUAL (m_receivedPacket2->GetSize (), 0);
00744
00745 m_receivedPacket->RemoveAllTags ();
00746 m_receivedPacket2->RemoveAllTags ();
00747
00748
00749
00750 m_receivedPacket = Create<Packet> ();
00751 m_receivedPacket2 = Create<Packet> ();
00752 NS_TEST_ASSERT_EQUAL (txSocket->SendTo ( Create<Packet> (123), 0,
00753 InetSocketAddress (Ipv4Address("255.255.255.255"), 1234)), 123);
00754 Simulator::Run ();
00755 NS_TEST_ASSERT_EQUAL (m_receivedPacket->GetSize (), 123);
00756
00757 NS_TEST_ASSERT_EQUAL (m_receivedPacket2->GetSize (), 0);
00758
00759 m_receivedPacket->RemoveAllTags ();
00760 m_receivedPacket2->RemoveAllTags ();
00761
00762
00763
00764
00765
00766
00767 rxSocket2->Dispose ();
00768 rxSocket2 = rxSocketFactory->CreateSocket ();
00769 rxSocket2->SetRecvCallback (MakeCallback (&UdpSocketImplTest::ReceivePkt2, this));
00770 NS_TEST_ASSERT_EQUAL (rxSocket2->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0"), 1234)), 0);
00771
00772 m_receivedPacket = Create<Packet> ();
00773 m_receivedPacket2 = Create<Packet> ();
00774 NS_TEST_ASSERT_EQUAL (txSocket->SendTo (Create<Packet> (123), 0,
00775 InetSocketAddress (Ipv4Address("255.255.255.255"), 1234)), 123);
00776 Simulator::Run ();
00777 NS_TEST_ASSERT_EQUAL (m_receivedPacket->GetSize (), 123);
00778 NS_TEST_ASSERT_EQUAL (m_receivedPacket2->GetSize (), 123);
00779
00780 m_receivedPacket->RemoveAllTags ();
00781 m_receivedPacket2->RemoveAllTags ();
00782
00783 Simulator::Destroy ();
00784
00785 return result;
00786 }
00787
00788 static UdpSocketImplTest gUdpSocketImplTest;
00789
00790 };
00791
00792 #endif