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