LEFT | RIGHT |
(no file at all) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2011 UPB |
| 4 * Copyright (c) 2017 NITK Surathkal |
| 5 * |
| 6 * This program is free software; you can redistribute it and/or modify |
| 7 * it under the terms of the GNU General Public License version 2 as |
| 8 * published by the Free Software Foundation; |
| 9 * |
| 10 * This program is distributed in the hope that it will be useful, |
| 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 * GNU General Public License for more details. |
| 14 * |
| 15 * You should have received a copy of the GNU General Public License |
| 16 * along with this program; if not, write to the Free Software |
| 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 * |
| 19 * Author: Radu Lupu <rlupu@elcom.pub.ro> |
| 20 * Ankit Deepak <adadeepak8@gmail.com> |
| 21 * Deepti Rajagopal <deeptir96@gmail.com> |
| 22 * |
| 23 */ |
| 24 |
| 25 #include <fstream> |
| 26 #include "ns3/core-module.h" |
| 27 #include "ns3/network-module.h" |
| 28 #include "ns3/internet-apps-module.h" |
| 29 #include "ns3/csma-module.h" |
| 30 #include "ns3/internet-module.h" |
| 31 #include "ns3/point-to-point-module.h" |
| 32 #include "ns3/applications-module.h" |
| 33 |
| 34 using namespace ns3; |
| 35 |
| 36 NS_LOG_COMPONENT_DEFINE ("DhcpExample"); |
| 37 |
| 38 int |
| 39 main (int argc, char *argv[]) |
| 40 { |
| 41 CommandLine cmd; |
| 42 cmd.Parse (argc, argv); |
| 43 |
| 44 GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true)); |
| 45 LogComponentEnable ("DhcpClient", LOG_LEVEL_INFO); |
| 46 LogComponentEnable ("DhcpServer", LOG_LEVEL_INFO); |
| 47 |
| 48 NS_LOG_INFO ("Create nodes."); |
| 49 NodeContainer MN; |
| 50 NodeContainer Router; |
| 51 MN.Create (3); |
| 52 Router.Create (2); |
| 53 |
| 54 NodeContainer net (MN, Router); |
| 55 |
| 56 NS_LOG_INFO ("Create channels."); |
| 57 CsmaHelper csma; |
| 58 csma.SetChannelAttribute ("DataRate", StringValue ("5Mbps")); |
| 59 csma.SetChannelAttribute ("Delay", StringValue ("2ms")); |
| 60 csma.SetDeviceAttribute ("Mtu", UintegerValue (1500)); |
| 61 NetDeviceContainer dev_net = csma.Install (net); |
| 62 |
| 63 NodeContainer p2pNodes; |
| 64 p2pNodes.Add (net.Get (4)); |
| 65 p2pNodes.Create (1); |
| 66 |
| 67 PointToPointHelper pointToPoint; |
| 68 pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); |
| 69 pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); |
| 70 |
| 71 NetDeviceContainer p2pDevices; |
| 72 p2pDevices = pointToPoint.Install (p2pNodes); |
| 73 |
| 74 InternetStackHelper tcpip; |
| 75 tcpip.Install (MN); |
| 76 tcpip.Install (Router); |
| 77 tcpip.Install (p2pNodes.Get (1)); |
| 78 |
| 79 Ipv4AddressHelper address; |
| 80 address.SetBase ("172.30.1.0", "255.255.255.0"); |
| 81 Ipv4InterfaceContainer p2pInterfaces; |
| 82 p2pInterfaces = address.Assign (p2pDevices); |
| 83 |
| 84 Ptr<Ipv4> ipv4MN = net.Get (0)->GetObject<Ipv4> (); |
| 85 uint32_t ifIndex = ipv4MN->AddInterface (dev_net.Get (0)); |
| 86 ipv4MN->AddAddress (ifIndex, Ipv4InterfaceAddress (Ipv4Address ("0.0.0.0"), Ip
v4Mask ("/0"))); |
| 87 ipv4MN->SetForwarding (ifIndex, true); |
| 88 ipv4MN->SetUp (ifIndex); |
| 89 |
| 90 Ptr<Ipv4> ipv4MN1 = net.Get (1)->GetObject<Ipv4> (); |
| 91 uint32_t ifIndex1 = ipv4MN1->AddInterface (dev_net.Get (1)); |
| 92 ipv4MN1->AddAddress (ifIndex1, Ipv4InterfaceAddress (Ipv4Address ("0.0.0.0"),
Ipv4Mask ("/0"))); |
| 93 ipv4MN1->SetForwarding (ifIndex1, true); |
| 94 ipv4MN1->SetUp (ifIndex1); |
| 95 |
| 96 Ptr<Ipv4> ipv4MN2 = net.Get (2)->GetObject<Ipv4> (); |
| 97 uint32_t ifIndex2 = ipv4MN2->AddInterface (dev_net.Get (2)); |
| 98 ipv4MN2->AddAddress (ifIndex2, Ipv4InterfaceAddress (Ipv4Address ("0.0.0.0"),
Ipv4Mask ("/0"))); |
| 99 ipv4MN2->SetForwarding (ifIndex2, true); |
| 100 ipv4MN2->SetUp (ifIndex2); |
| 101 |
| 102 Ptr<Ipv4> ipv4Router = net.Get (3)->GetObject<Ipv4> (); |
| 103 ifIndex = ipv4Router->AddInterface (dev_net.Get (3)); |
| 104 ipv4Router->AddAddress (ifIndex, Ipv4InterfaceAddress (Ipv4Address ("172.30.0.
12"), Ipv4Mask ("/0"))); // need to remove this workaround |
| 105 ipv4Router->AddAddress (ifIndex, Ipv4InterfaceAddress (Ipv4Address ("172.30.0.
12"), Ipv4Mask ("/24"))); |
| 106 ipv4Router->SetForwarding (ifIndex, true); |
| 107 ipv4Router->SetUp (ifIndex); |
| 108 |
| 109 Ptr<Ipv4> ipv4Router1 = net.Get (4)->GetObject<Ipv4> (); |
| 110 ifIndex = ipv4Router1->AddInterface (dev_net.Get (4)); |
| 111 ipv4Router1->AddAddress (ifIndex, Ipv4InterfaceAddress (Ipv4Address ("172.30.0
.17"), Ipv4Mask ("/0"))); // need to remove this workaround |
| 112 ipv4Router1->AddAddress (ifIndex, Ipv4InterfaceAddress (Ipv4Address ("172.30.0
.17"), Ipv4Mask ("/24"))); |
| 113 ipv4Router1->SetForwarding (ifIndex, true); |
| 114 ipv4Router1->SetUp (ifIndex); |
| 115 |
| 116 NS_LOG_INFO ("Create Applications."); |
| 117 |
| 118 DhcpServerHelper dhcp_server (Ipv4Address ("172.30.0.0"), Ipv4Mask ("/24"), Ip
v4Address ("172.30.0.12"), Ipv4Address ("172.30.0.10"), Ipv4Address ("172.30.0.1
5"), Ipv4Address ("172.30.0.17")); |
| 119 ApplicationContainer ap_dhcp_server = dhcp_server.Install (Router.Get (0)); |
| 120 ap_dhcp_server.Start (Seconds (1.0)); |
| 121 ap_dhcp_server.Stop (Seconds (500.0)); |
| 122 |
| 123 DhcpClientHelper dhcp_client (0); |
| 124 ApplicationContainer ap_dhcp_client = dhcp_client.Install (MN.Get (0)); |
| 125 ap_dhcp_client.Start (Seconds (1.0)); |
| 126 ap_dhcp_client.Stop (Seconds (100.0)); |
| 127 |
| 128 ApplicationContainer ap_dhcp_client3 = dhcp_client.Install (MN.Get (0)); |
| 129 ap_dhcp_client3.Start (Seconds (400.0)); |
| 130 ap_dhcp_client3.Stop (Seconds (500.0)); |
| 131 |
| 132 DhcpClientHelper dhcp_client1 (0); |
| 133 ApplicationContainer ap_dhcp_client1 = dhcp_client1.Install (MN.Get (1)); |
| 134 ap_dhcp_client1.Start (Seconds (1.0)); |
| 135 ap_dhcp_client1.Stop (Seconds (500.0)); |
| 136 |
| 137 DhcpClientHelper dhcp_client2 (0); |
| 138 ApplicationContainer ap_dhcp_client2 = dhcp_client2.Install (MN.Get (2)); |
| 139 ap_dhcp_client2.Start (Seconds (1.0)); |
| 140 ap_dhcp_client2.Stop (Seconds (500.0)); |
| 141 |
| 142 csma.EnablePcapAll ("dhcp"); |
| 143 |
| 144 UdpEchoServerHelper echoServer (9); |
| 145 |
| 146 ApplicationContainer serverApps = echoServer.Install (p2pNodes.Get (1)); |
| 147 serverApps.Start (Seconds (1.0)); |
| 148 serverApps.Stop (Seconds (100.0)); |
| 149 |
| 150 UdpEchoClientHelper echoClient (p2pInterfaces.GetAddress (1), 9); |
| 151 echoClient.SetAttribute ("MaxPackets", UintegerValue (100)); |
| 152 echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); |
| 153 echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); |
| 154 |
| 155 ApplicationContainer clientApps = echoClient.Install (MN.Get (1)); |
| 156 clientApps.Start (Seconds (2.0)); |
| 157 clientApps.Stop (Seconds (100.0)); |
| 158 |
| 159 Simulator::Stop (Seconds (500.0)); |
| 160 |
| 161 NS_LOG_INFO ("Run Simulation."); |
| 162 Simulator::Run (); |
| 163 Simulator::Destroy (); |
| 164 NS_LOG_INFO ("Done."); |
| 165 } |
LEFT | RIGHT |