hello,
I referred so many post about assigning Ip address but any of them didnt work.. I have code attached below. Kindly anyone help me to get ip address for all the received packets.
// Default Network Topology
//
// Wifi 10.1.3.0
// AP
// * * * *
// | | | | 10.1.1.0
// n5 n6 n7 n0 -------------- n1 n2 n3 n4
// point-to-point | | | |
// ================
// LAN 10.1.2.0
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("Pm3Example");
double firstRxTime = -1.0, lastRxTime;
uint32_t bytesTotal = 0;
/*
void SinkRxTrace(Ptr<const Packet> pkt, const Address &addr)
//void SinkRxTrace(std::string context ,Ptr<const Packet> pkt, const Address &addr)
{
if (firstRxTime < 0)
firstRxTime = Simulator::Now().GetSeconds();
lastRxTime = Simulator::Now().GetSeconds();
bytesTotal += pkt->GetSize();
//std::cout << context << "123 " << rtt << std::endl;
std::cout<<"test"<<std::endl;
}*/
int pktcounter=0;
void ReceivedPacket(Ptr<const Packet> packet ,const Address &)
{
NS_LOG_UNCOND("Received one Packet ");
Ipv4Address address;
Ipv4Header ipv4;
address = ipv4.GetDestination();
//std::cout<<"IP:"<<pktcounter<<std::endl;
//packet->>
pktcounter++; //defined before
std::cout<<"packet number is :"<<pktcounter<<std::endl;
/*IP address for the receiving packet
void Add (Ptr<Node> node);
NodeContainer (Ptr< Node > node)
Ptr<Node> node = nodes.Get (3); // Get pointer to ith node in container
Ptr<Ipv4> Ptr1 = node->GetObject<Ipv4> (); // Get Ipv4 instance of the node
Ipv4Address addr = ipv4->GetAddress (1, 0).GetLocal (); // Get Ipv4InterfaceAddress of xth interface. */
//IP address for the receiving packet
std::cout << "Received one packet! Socket: " << ipv4.GetSource() << std::endl;" port: " << ipv4.GetPort ();
}
int
main (int argc, char *argv[])
{
bool verbose = true;
uint32_t nCsma = 3;
uint32_t nWifi = 3;
CommandLine cmd;
cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi);
cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);
cmd.Parse (argc,argv);
if (nWifi > 18)
{
std::cout << "Number of wifi nodes " << nWifi <<
" specified exceeds the mobility bounding box" << std::endl;
exit (1);
}
if (verbose)
{
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
}
NodeContainer p2pNodes;
p2pNodes.Create (2);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
NetDeviceContainer p2pDevices;
p2pDevices = pointToPoint.Install (p2pNodes);
NodeContainer csmaNodes;
csmaNodes.Add (p2pNodes.Get (1));
csmaNodes.Create (nCsma);
CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
NetDeviceContainer csmaDevices;
csmaDevices = csma.Install (csmaNodes);
NodeContainer wifiStaNodes;
wifiStaNodes.Create (nWifi);
NodeContainer wifiApNode = p2pNodes.Get (0);
YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
phy.SetChannel (channel.Create ());
WifiHelper wifi = WifiHelper::Default ();
wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
NqosWifiMacHelper mac = NqosWifiMacHelper::Default ();
Ssid ssid = Ssid ("ns-3-ssid");
mac.SetType ("ns3::StaWifiMac",
"Ssid", SsidValue (ssid),
"ActiveProbing", BooleanValue (false));
NetDeviceContainer staDevices;
staDevices = wifi.Install (phy, mac, wifiStaNodes);
mac.SetType ("ns3::ApWifiMac",
"Ssid", SsidValue (ssid));
NetDeviceContainer apDevices;
apDevices = wifi.Install (phy, mac, wifiApNode);
MobilityHelper mobility;
mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", DoubleValue (0.0),
"MinY", DoubleValue (0.0),
"DeltaX", DoubleValue (5.0),
"DeltaY", DoubleValue (10.0),
"GridWidth", UintegerValue (3),
"LayoutType", StringValue ("RowFirst"));
mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
"Bounds", RectangleValue (Rectangle (-50, 50, -50, 50)));
mobility.Install (wifiStaNodes);
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (wifiApNode);
InternetStackHelper stack;
stack.Install (csmaNodes);
stack.Install (wifiApNode);
stack.Install (wifiStaNodes);
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer p2pInterfaces;
p2pInterfaces = address.Assign (p2pDevices);
address.SetBase ("10.1.2.0", "255.255.255.0");
Ipv4InterfaceContainer csmaInterfaces;
csmaInterfaces = address.Assign (csmaDevices);
address.SetBase ("10.1.3.0", "255.255.255.0");
address.Assign (staDevices);
address.Assign (apDevices);
uint16_t port = 9;
OnOffHelper onoff(
"ns3::UdpSocketFactory",
InetSocketAddress("10.1.2.4", port));
onoff.SetAttribute("OnTime",StringValue("ns3::ConstantRandomVariable[Constant=1]"));
onoff.SetAttribute("OffTime",StringValue("ns3::ConstantRandomVariable[Constant=0]"));
onoff.SetAttribute("DataRate", StringValue("512bps"));
onoff.SetAttribute("PacketSize", StringValue("512"));
onoff.Install(wifiStaNodes.Get(nWifi - 1));
ApplicationContainer apps = onoff.Install(wifiStaNodes.Get(nWifi-1));
//ApplicationContainer apps = onoff.Install(wifiStaNodes.Get(2));
apps.Start(Seconds(5.0));
apps.Stop(Seconds(7.0));
PacketSinkHelper sink("ns3::UdpSocketFactory",InetSocketAddress("10.1.2.4", port));
sink.Install(csmaNodes.Get(nCsma));
Simulator::Stop(Seconds(25.0));
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
pointToPoint.EnablePcapAll ("pm-ex3");
phy.EnablePcap ("pm-ex3", apDevices.Get (0));
csma.EnablePcap ("pm-ex3", csmaDevices.Get (0), true);
//Config::ConnectWithoutContext(
// "/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
//MakeCallback(&SinkRxTrace));
Config::ConnectWithoutContext("NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",MakeCallback(&ReceivedPacket));
// Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
// MakeCallback (&SinkRxTrace));
csma.EnablePcapAll ("pm-ex3", false);
Simulator::Run ();
std::cout << "Avg throughput = "
<< bytesTotal*8/(lastRxTime-firstRxTime)/1024
<< " kbits/sec" << std::endl;
Simulator::Destroy ();