int main (int argc, char *argv[]) { // // Users may find it convenient to turn on explicit debugging // for selected modules; the below lines suggest how to do this // #if 0 LogComponentEnable ("UdpEchoExample", LOG_LEVEL_INFO); LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_ALL); LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_ALL); #endif // // Allow the user to override any of the defaults and the above Bind() at // run-time, via command-line arguments // bool useV6 = false; Address serverAddress; CommandLine cmd; cmd.AddValue ("useIpv6", "Use Ipv6", useV6); cmd.Parse (argc, argv); // // Explicitly create the nodes required by the topology (shown above). // NS_LOG_INFO ("Create nodes."); NodeContainer n; n.Create (4); InternetStackHelper internet; internet.Install (n); NS_LOG_INFO ("Create channels."); // // Explicitly create the channels required by the topology (shown above). // CsmaHelper csma; csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000))); csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2))); csma.SetDeviceAttribute ("Mtu", UintegerValue (1400)); NetDeviceContainer d = csma.Install (n); // // We've got the "hardware" in place. Now we need to add IP addresses. // NS_LOG_INFO ("Assign IP Addresses."); if (useV6 == false) { Ipv4AddressHelper ipv4; ipv4.SetBase ("10.1.1.0", "255.255.255.0"); Ipv4InterfaceContainer i = ipv4.Assign (d); serverAddress = Address(i.GetAddress (1)); } else { Ipv6AddressHelper ipv6; ipv6.SetBase ("2001:0000:f00d:cafe::", Ipv6Prefix (64)); Ipv6InterfaceContainer i6 = ipv6.Assign (d); serverAddress = Address(i6.GetAddress (1,1)); } NS_LOG_INFO ("Create Applications."); // // Create a UdpEchoServer application on node one. // uint16_t port = 9; // well-known echo port number UdpEchoServerHelper server (port); ApplicationContainer apps = server.Install (n.Get (1)); apps.Start (Seconds (1.0)); apps.Stop (Seconds (10.0)); // // Create a UdpEchoClient application to send UDP datagrams from node zero to // node one. // uint32_t packetSize = 1024; uint32_t maxPacketCount = 1; Time interPacketInterval = Seconds (1.); UdpEchoClientHelper client (serverAddress, port); client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); client.SetAttribute ("Interval", TimeValue (interPacketInterval)); client.SetAttribute ("PacketSize", UintegerValue (packetSize)); apps = client.Install (n.Get (0)); apps.Start (Seconds (2.0)); apps.Stop (Seconds (10.0)); #if 0 // // Users may find it convenient to initialize echo packets with actual data; // the below lines suggest how to do this // client.SetFill (apps.Get (0), "Hello World"); client.SetFill (apps.Get (0), 0xa5, 1024); uint8_t fill[] = { 0, 1, 2, 3, 4, 5, 6}; client.SetFill (apps.Get (0), fill, sizeof(fill), 1024); #endif AsciiTraceHelper ascii; csma.EnableAsciiAll (ascii.CreateFileStream ("udp-echo.tr")); csma.EnablePcapAll ("udp-echo", false); // // Now, do the actual simulation. // NS_LOG_INFO ("Run Simulation."); Simulator::Run (); Simulator::Destroy (); NS_LOG_INFO ("Done."); }
int main (int argc, char *argv[]) { // // Users may find it convenient to turn on explicit debugging // for selected modules; the below lines suggest how to do this // #if 0 LogComponentEnable ("CsmaBridgeOneHopExample", LOG_LEVEL_INFO); #endif // // Allow the user to override any of the defaults and the above Bind() at // run-time, via command-line arguments // CommandLine cmd; cmd.Parse (argc, argv); // // Explicitly create the nodes required by the topology (shown above). // NS_LOG_INFO ("Create nodes."); Ptr<Node> n0 = CreateObject<Node> (); Ptr<Node> n1 = CreateObject<Node> (); Ptr<Node> n2 = CreateObject<Node> (); Ptr<Node> n3 = CreateObject<Node> (); Ptr<Node> n4 = CreateObject<Node> (); Ptr<Node> bridge1 = CreateObject<Node> (); Ptr<Node> bridge2 = CreateObject<Node> (); NS_LOG_INFO ("Build Topology"); CsmaHelper csma; csma.SetChannelAttribute ("DataRate", DataRateValue (5000000)); csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2))); // Create the csma links, from each terminal to the bridge // This will create six network devices; we'll keep track separately // of the devices on and off the bridge respectively, for later configuration NetDeviceContainer topLanDevices; NetDeviceContainer topBridgeDevices; // It is easier to iterate the nodes in C++ if we put them into a container NodeContainer topLan (n2, n0, n1); for (int i = 0; i < 3; i++) { // install a csma channel between the ith toplan node and the bridge node NetDeviceContainer link = csma.Install (NodeContainer (topLan.Get (i), bridge1)); topLanDevices.Add (link.Get (0)); topBridgeDevices.Add (link.Get (1)); } // // Now, Create the bridge netdevice, which will do the packet switching. The // bridge lives on the node bridge1 and bridges together the topBridgeDevices // which are the three CSMA net devices on the node in the diagram above. // BridgeHelper bridge; bridge.Install (bridge1, topBridgeDevices); // Add internet stack to the router nodes NodeContainer routerNodes (n0, n1, n2, n3, n4); InternetStackHelper internet; internet.Install (routerNodes); // Repeat for bottom bridged LAN NetDeviceContainer bottomLanDevices; NetDeviceContainer bottomBridgeDevices; NodeContainer bottomLan (n2, n3, n4); for (int i = 0; i < 3; i++) { NetDeviceContainer link = csma.Install (NodeContainer (bottomLan.Get (i), bridge2)); bottomLanDevices.Add (link.Get (0)); bottomBridgeDevices.Add (link.Get (1)); } bridge.Install (bridge2, bottomBridgeDevices); // We've got the "hardware" in place. Now we need to add IP addresses. NS_LOG_INFO ("Assign IP Addresses."); Ipv4AddressHelper ipv4; ipv4.SetBase ("10.1.1.0", "255.255.255.0"); ipv4.Assign (topLanDevices); ipv4.SetBase ("10.1.2.0", "255.255.255.0"); ipv4.Assign (bottomLanDevices); // // Create router nodes, initialize routing database and set up the routing // tables in the nodes. We excuse the bridge nodes from having to serve as // routers, since they don't even have internet stacks on them. // Ipv4GlobalRoutingHelper::PopulateRoutingTables (); // // Create an OnOff application to send UDP datagrams from node zero to node 1. // NS_LOG_INFO ("Create Applications."); uint16_t port = 9; // Discard port (RFC 863) OnOffHelper onoff ("ns3::UdpSocketFactory", Address (InetSocketAddress (Ipv4Address ("10.1.1.3"), port))); onoff.SetConstantRate (DataRate ("500kb/s")); ApplicationContainer app = onoff.Install (n0); // Start the application app.Start (Seconds (1.0)); app.Stop (Seconds (10.0)); // Create an optional packet sink to receive these packets PacketSinkHelper sink ("ns3::UdpSocketFactory", Address (InetSocketAddress (Ipv4Address::GetAny (), port))); ApplicationContainer sink1 = sink.Install (n1); sink1.Start (Seconds (1.0)); sink1.Stop (Seconds (10.0)); // // Create a similar flow from n3 to n0, starting at time 1.1 seconds // onoff.SetAttribute ("Remote", AddressValue (InetSocketAddress (Ipv4Address ("10.1.1.2"), port))); ApplicationContainer app2 = onoff.Install (n3); app2.Start (Seconds (1.1)); app2.Stop (Seconds (10.0)); ApplicationContainer sink2 = sink.Install (n0); sink2.Start (Seconds (1.1)); sink2.Stop (Seconds (10.0)); NS_LOG_INFO ("Configure Tracing."); // // Configure tracing of all enqueue, dequeue, and NetDevice receive events. // Trace output will be sent to the file "csma-bridge-one-hop.tr" // AsciiTraceHelper ascii; csma.EnableAsciiAll (ascii.CreateFileStream ("csma-bridge-one-hop.tr")); // // Also configure some tcpdump traces; each interface will be traced. // The output files will be named: // csma-bridge-one-hop-<nodeId>-<interfaceId>.pcap // and can be read by the "tcpdump -r" command (use "-tt" option to // display timestamps correctly) // csma.EnablePcapAll ("csma-bridge-one-hop", false); // // Now, do the actual simulation. // NS_LOG_INFO ("Run Simulation."); Simulator::Run (); Simulator::Destroy (); NS_LOG_INFO ("Done."); }
int main (int argc, char *argv[]) { // // Users may find it convenient to turn on explicit debugging // for selected modules; the below lines suggest how to do this // // LogComponentEnable ("CsmaMulticastExample", LOG_LEVEL_INFO); // // Set up default values for the simulation. // // Select DIX/Ethernet II-style encapsulation (no LLC/Snap header) Config::SetDefault ("ns3::CsmaNetDevice::EncapsulationMode", StringValue ("Dix")); // Allow the user to override any of the defaults at // run-time, via command-line arguments CommandLine cmd; cmd.Parse (argc, argv); NS_LOG_INFO ("Create nodes."); NodeContainer c; c.Create (5); // We will later want two subcontainers of these nodes, for the two LANs NodeContainer c0 = NodeContainer (c.Get (0), c.Get (1), c.Get (2)); NodeContainer c1 = NodeContainer (c.Get (2), c.Get (3), c.Get (4)); NS_LOG_INFO ("Build Topology."); CsmaHelper csma; csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000))); csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2))); // We will use these NetDevice containers later, for IP addressing NetDeviceContainer nd0 = csma.Install (c0); // First LAN NetDeviceContainer nd1 = csma.Install (c1); // Second LAN NS_LOG_INFO ("Add IP Stack."); InternetStackHelper internet; internet.Install (c); NS_LOG_INFO ("Assign IP Addresses."); Ipv4AddressHelper ipv4Addr; ipv4Addr.SetBase ("10.1.1.0", "255.255.255.0"); ipv4Addr.Assign (nd0); ipv4Addr.SetBase ("10.1.2.0", "255.255.255.0"); ipv4Addr.Assign (nd1); NS_LOG_INFO ("Configure multicasting."); // // Now we can configure multicasting. As described above, the multicast // source is at node zero, which we assigned the IP address of 10.1.1.1 // earlier. We need to define a multicast group to send packets to. This // can be any multicast address from 224.0.0.0 through 239.255.255.255 // (avoiding the reserved routing protocol addresses). // Ipv4Address multicastSource ("10.1.1.1"); Ipv4Address multicastGroup ("225.1.2.4"); // Now, we will set up multicast routing. We need to do three things: // 1) Configure a (static) multicast route on node n2 // 2) Set up a default multicast route on the sender n0 // 3) Have node n4 join the multicast group // We have a helper that can help us with static multicast Ipv4StaticRoutingHelper multicast; // 1) Configure a (static) multicast route on node n2 (multicastRouter) Ptr<Node> multicastRouter = c.Get (2); // The node in question Ptr<NetDevice> inputIf = nd0.Get (2); // The input NetDevice NetDeviceContainer outputDevices; // A container of output NetDevices outputDevices.Add (nd1.Get (0)); // (we only need one NetDevice here) multicast.AddMulticastRoute (multicastRouter, multicastSource, multicastGroup, inputIf, outputDevices); // 2) Set up a default multicast route on the sender n0 Ptr<Node> sender = c.Get (0); Ptr<NetDevice> senderIf = nd0.Get (0); multicast.SetDefaultMulticastRoute (sender, senderIf); // // Create an OnOff application to send UDP datagrams from node zero to the // multicast group (node four will be listening). // NS_LOG_INFO ("Create Applications."); uint16_t multicastPort = 9; // Discard port (RFC 863) // Configure a multicast packet generator that generates a packet // every few seconds OnOffHelper onoff ("ns3::UdpSocketFactory", Address (InetSocketAddress (multicastGroup, multicastPort))); onoff.SetConstantRate (DataRate ("255b/s")); onoff.SetAttribute ("PacketSize", UintegerValue (128)); ApplicationContainer srcC = onoff.Install (c0.Get (0)); // // Tell the application when to start and stop. // srcC.Start (Seconds (1.)); srcC.Stop (Seconds (10.)); // Create an optional packet sink to receive these packets PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), multicastPort)); ApplicationContainer sinkC = sink.Install (c1.Get (2)); // Node n4 // Start the sink sinkC.Start (Seconds (1.0)); sinkC.Stop (Seconds (10.0)); NS_LOG_INFO ("Configure Tracing."); // // Configure tracing of all enqueue, dequeue, and NetDevice receive events. // Ascii trace output will be sent to the file "csma-multicast.tr" // AsciiTraceHelper ascii; csma.EnableAsciiAll (ascii.CreateFileStream ("csma-multicast.tr")); // Also configure some tcpdump traces; each interface will be traced. // The output files will be named: // csma-multicast-<nodeId>-<interfaceId>.pcap // and can be read by the "tcpdump -r" command (use "-tt" option to // display timestamps correctly) csma.EnablePcapAll ("csma-multicast", false); // // Now, do the actual simulation. // NS_LOG_INFO ("Run Simulation."); Simulator::Run (); Simulator::Destroy (); NS_LOG_INFO ("Done."); }
int main (int argc, char** argv) { bool verbose = false; CommandLine cmd; cmd.AddValue ("verbose", "turn on log components", verbose); cmd.Parse (argc, argv); if (verbose) { LogComponentEnable ("Ipv6L3Protocol", LOG_LEVEL_ALL); LogComponentEnable ("Icmpv6L4Protocol", LOG_LEVEL_ALL); LogComponentEnable ("Ipv6StaticRouting", LOG_LEVEL_ALL); LogComponentEnable ("Ipv6Interface", LOG_LEVEL_ALL); LogComponentEnable ("Ping6Application", LOG_LEVEL_ALL); } NS_LOG_INFO ("Create nodes."); Ptr<Node> n0 = CreateObject<Node> (); Ptr<Node> r = CreateObject<Node> (); Ptr<Node> n1 = CreateObject<Node> (); NodeContainer net1 (n0, r); NodeContainer net2 (r, n1); NodeContainer all (n0, r, n1); NS_LOG_INFO ("Create IPv6 Internet Stack"); InternetStackHelper internetv6; internetv6.Install (all); NS_LOG_INFO ("Create channels."); CsmaHelper csma; csma.SetChannelAttribute ("DataRate", DataRateValue (5000000)); csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2))); NetDeviceContainer d1 = csma.Install (net1); NetDeviceContainer d2 = csma.Install (net2); NS_LOG_INFO ("Create networks and assign IPv6 Addresses."); Ipv6AddressHelper ipv6; ipv6.SetBase (Ipv6Address ("2001:1::"), Ipv6Prefix (64)); Ipv6InterfaceContainer i1 = ipv6.Assign (d1); i1.SetForwarding (1, true); i1.SetDefaultRouteInAllNodes (1); ipv6.SetBase (Ipv6Address ("2001:2::"), Ipv6Prefix (64)); Ipv6InterfaceContainer i2 = ipv6.Assign (d2); i2.SetForwarding (0, true); i2.SetDefaultRouteInAllNodes (0); Ipv6StaticRoutingHelper routingHelper; Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> (&std::cout); routingHelper.PrintRoutingTableAt (Seconds (0), n0, routingStream); /* Create a Ping6 application to send ICMPv6 echo request from n0 to n1 via r */ uint32_t packetSize = 4096; uint32_t maxPacketCount = 5; Time interPacketInterval = Seconds (1.0); Ping6Helper ping6; ping6.SetLocal (i1.GetAddress (0, 1)); ping6.SetRemote (i2.GetAddress (1, 1)); ping6.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); ping6.SetAttribute ("Interval", TimeValue (interPacketInterval)); ping6.SetAttribute ("PacketSize", UintegerValue (packetSize)); ApplicationContainer apps = ping6.Install (net1.Get (0)); apps.Start (Seconds (2.0)); apps.Stop (Seconds (20.0)); AsciiTraceHelper ascii; csma.EnableAsciiAll (ascii.CreateFileStream ("fragmentation-ipv6.tr")); csma.EnablePcapAll (std::string ("fragmentation-ipv6"), true); NS_LOG_INFO ("Run Simulation."); Simulator::Run (); Simulator::Destroy (); NS_LOG_INFO ("Done."); }