scratch/netfilter-example.cc
author Qasim Javed <qasimj@gmail.com>
Thu Aug 06 11:16:51 2009 +0600 (2009-08-06)
changeset 4639 a4a59bd1ac1c
permissions -rw-r--r--
Added missing file(s)
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * This program is free software; you can redistribute it and/or modify
     4  * it under the terms of the GNU General Public License version 2 as
     5  * published by the Free Software Foundation;
     6  *
     7  * This program is distributed in the hope that it will be useful,
     8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    10  * GNU General Public License for more details.
    11  *
    12  * You should have received a copy of the GNU General Public License
    13  * along with this program; if not, write to the Free Software
    14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    15  */
    16 
    17 #include "ns3/core-module.h"
    18 #include "ns3/simulator-module.h"
    19 #include "ns3/node-module.h"
    20 #include "ns3/helper-module.h"
    21 #include "ns3/global-route-manager.h"
    22 #include "ns3/on-off-helper.h"
    23 #include "ns3/v4ping-helper.h"
    24 #include "ns3/ipv4-l3-protocol.h"
    25 
    26 using namespace ns3;
    27 
    28 NS_LOG_COMPONENT_DEFINE ("NetfilterExample");
    29 
    30   int 
    31 main (int argc, char *argv[])
    32 {
    33   LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
    34   LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
    35 
    36   uint16_t port = 9;
    37 
    38   NodeContainer first;
    39   first.Create (2);
    40 
    41   NodeContainer second;
    42   second.Add ( first.Get (1) );
    43   second.Create (1);
    44 
    45   Packet::EnablePrinting();
    46 
    47   PointToPointHelper pointToPoint;
    48   pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
    49   pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
    50 
    51 
    52   //V4PingHelper ping ( Ipv4Address ("10.2.2.2"));
    53 
    54   NetDeviceContainer devices1;
    55   devices1 = pointToPoint.Install (first);
    56   
    57   NetDeviceContainer devices2;
    58   devices2 = pointToPoint.Install (second);
    59 
    60   InternetStackHelper stack;
    61   stack.Install (first);
    62   stack.Install (second.Get(1));
    63 
    64   Ipv4AddressHelper address1;
    65   address1.SetBase ("192.168.1.0", "255.255.255.0");
    66   
    67   Ipv4AddressHelper address2;
    68   address2.SetBase ("203.82.48.0", "255.255.255.0");
    69 
    70   Ipv4InterfaceContainer firstInterfaces = address1.Assign (devices1);
    71   Ipv4InterfaceContainer secondInterfaces = address2.Assign (devices2);
    72 
    73   Ptr <Ipv4> ipv4 = first.Get (1)->GetObject<Ipv4> ();
    74   std::cout << "Number of interfaces on node " << first.Get (1)->GetId () << ": " << ipv4->GetNInterfaces () << std::endl;
    75   //std::cout << "Device number: " << first.Get (1)->GetDevice (1) << std::endl;
    76 
    77   //int32_t ifNumber = ipv4->GetInterfaceForAddress ( Ipv4Address ("203.82.48.1"));
    78   //Ptr<Ipv4Interface> ipv4Interface = ipv4->GetInterface ( ifNumber );
    79   Ptr <Ipv4L3Protocol> ipv4L3 = DynamicCast <Ipv4L3Protocol>(first.Get (1)->GetObject<Ipv4> ());
    80   Ipv4Netfilter *netfilter = ipv4L3->GetNetfilter ();
    81   netfilter->EnableNat ();
    82   std::cout << "Adding rule at node " << first.Get (1)->GetId () << ", device " << first.Get (1)->GetDevice (1)->GetIfIndex () << std::endl;
    83   netfilter->AddNatRule ( NatRule (Ipv4Address ("192.168.1.1"), Ipv4Address ("203.82.48.1"), first.Get (1)->GetDevice (1)));
    84   //netfilter.SetAttribute ("EnableNat", 1);
    85 
    86   UdpEchoServerHelper echoServer (9);
    87 
    88   ApplicationContainer serverApps = echoServer.Install (second.Get (1));
    89   serverApps.Start (Seconds (1.0));
    90   serverApps.Stop (Seconds (10.0));
    91 
    92   UdpEchoClientHelper echoClient (secondInterfaces.GetAddress (1), 9);
    93   echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
    94   echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.)));
    95   echoClient.SetAttribute ("PacketSize", UintegerValue (512));
    96 
    97   ApplicationContainer clientApps = echoClient.Install (first.Get (0));
    98   clientApps.Start (Seconds (2.0));
    99   clientApps.Stop (Seconds (10.0));
   100 
   101   OnOffHelper onOff ( "ns3::TcpSocketFactory", 
   102                       Address (InetSocketAddress ("192.168.1.1", port)));
   103   onOff.SetAttribute ("MaxBytes", UintegerValue (512));
   104 
   105   ApplicationContainer onOffApp = onOff.Install (second.Get (1));
   106 
   107   PacketSinkHelper packetSink ( "ns3::TcpSocketFactory",
   108                                 Address (InetSocketAddress ("192.168.1.1", port)));
   109 
   110   ApplicationContainer packetSinkApp = packetSink.Install (first.Get (0));
   111 
   112   /*packetSinkApp.Start (Seconds (2.0));
   113   packetSinkApp.Stop (Seconds (4.0));
   114 
   115   onOffApp.Start (Seconds (3.0));
   116   onOffApp.Stop (Seconds (4.0));*/
   117 
   118   /*ApplicationContainer pingApp = ping.Install (first.Get (0));
   119   pingApp.Start (Seconds (3.0));
   120   pingApp.Stop (Seconds (4.0));*/
   121 
   122   GlobalRouteManager::PopulateRoutingTables ();
   123 
   124   PointToPointHelper::EnablePcapAll ("netfilter");
   125 
   126   Simulator::Run ();
   127   Simulator::Destroy ();
   128   return 0;
   129 }