how to get source IP address of received packet

1,163 views
Skip to first unread message

nipa patani

unread,
Feb 20, 2017, 5:17:38 AM2/20/17
to ns-3-users
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 ();

Konstantinos

unread,
Feb 20, 2017, 5:50:56 AM2/20/17
to ns-3-users
Hi Nipa,

First and foremost please follow the posting guidelines when you ask a question in the list. 
Specifically, when you provide an example code, do not post it within the body of your message, give the scenario as an attached file that can run on the latest ns-3 release
When I tried to run yours, it was not working at all. 

Regarding your question, in your callback you have an Address variable. This is the (Inet) address of the sender.
See the PacketSink application how this is handled. If you enable Logging on PacketSink you will get the output without the need of your callback.

Regards,
K

nipa patani

unread,
Feb 20, 2017, 11:56:06 PM2/20/17
to ns-3-users

Thanks for the quick reply.I will surely take in action about posting guidelines..
Reply all
Reply to author
Forward
0 new messages