1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/scratch/netfilter-example.cc Thu Aug 06 11:16:51 2009 +0600
1.3 @@ -0,0 +1,129 @@
1.4 +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
1.5 +/*
1.6 + * This program is free software; you can redistribute it and/or modify
1.7 + * it under the terms of the GNU General Public License version 2 as
1.8 + * published by the Free Software Foundation;
1.9 + *
1.10 + * This program is distributed in the hope that it will be useful,
1.11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.13 + * GNU General Public License for more details.
1.14 + *
1.15 + * You should have received a copy of the GNU General Public License
1.16 + * along with this program; if not, write to the Free Software
1.17 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.18 + */
1.19 +
1.20 +#include "ns3/core-module.h"
1.21 +#include "ns3/simulator-module.h"
1.22 +#include "ns3/node-module.h"
1.23 +#include "ns3/helper-module.h"
1.24 +#include "ns3/global-route-manager.h"
1.25 +#include "ns3/on-off-helper.h"
1.26 +#include "ns3/v4ping-helper.h"
1.27 +#include "ns3/ipv4-l3-protocol.h"
1.28 +
1.29 +using namespace ns3;
1.30 +
1.31 +NS_LOG_COMPONENT_DEFINE ("NetfilterExample");
1.32 +
1.33 + int
1.34 +main (int argc, char *argv[])
1.35 +{
1.36 + LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
1.37 + LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
1.38 +
1.39 + uint16_t port = 9;
1.40 +
1.41 + NodeContainer first;
1.42 + first.Create (2);
1.43 +
1.44 + NodeContainer second;
1.45 + second.Add ( first.Get (1) );
1.46 + second.Create (1);
1.47 +
1.48 + Packet::EnablePrinting();
1.49 +
1.50 + PointToPointHelper pointToPoint;
1.51 + pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
1.52 + pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
1.53 +
1.54 +
1.55 + //V4PingHelper ping ( Ipv4Address ("10.2.2.2"));
1.56 +
1.57 + NetDeviceContainer devices1;
1.58 + devices1 = pointToPoint.Install (first);
1.59 +
1.60 + NetDeviceContainer devices2;
1.61 + devices2 = pointToPoint.Install (second);
1.62 +
1.63 + InternetStackHelper stack;
1.64 + stack.Install (first);
1.65 + stack.Install (second.Get(1));
1.66 +
1.67 + Ipv4AddressHelper address1;
1.68 + address1.SetBase ("192.168.1.0", "255.255.255.0");
1.69 +
1.70 + Ipv4AddressHelper address2;
1.71 + address2.SetBase ("203.82.48.0", "255.255.255.0");
1.72 +
1.73 + Ipv4InterfaceContainer firstInterfaces = address1.Assign (devices1);
1.74 + Ipv4InterfaceContainer secondInterfaces = address2.Assign (devices2);
1.75 +
1.76 + Ptr <Ipv4> ipv4 = first.Get (1)->GetObject<Ipv4> ();
1.77 + std::cout << "Number of interfaces on node " << first.Get (1)->GetId () << ": " << ipv4->GetNInterfaces () << std::endl;
1.78 + //std::cout << "Device number: " << first.Get (1)->GetDevice (1) << std::endl;
1.79 +
1.80 + //int32_t ifNumber = ipv4->GetInterfaceForAddress ( Ipv4Address ("203.82.48.1"));
1.81 + //Ptr<Ipv4Interface> ipv4Interface = ipv4->GetInterface ( ifNumber );
1.82 + Ptr <Ipv4L3Protocol> ipv4L3 = DynamicCast <Ipv4L3Protocol>(first.Get (1)->GetObject<Ipv4> ());
1.83 + Ipv4Netfilter *netfilter = ipv4L3->GetNetfilter ();
1.84 + netfilter->EnableNat ();
1.85 + std::cout << "Adding rule at node " << first.Get (1)->GetId () << ", device " << first.Get (1)->GetDevice (1)->GetIfIndex () << std::endl;
1.86 + netfilter->AddNatRule ( NatRule (Ipv4Address ("192.168.1.1"), Ipv4Address ("203.82.48.1"), first.Get (1)->GetDevice (1)));
1.87 + //netfilter.SetAttribute ("EnableNat", 1);
1.88 +
1.89 + UdpEchoServerHelper echoServer (9);
1.90 +
1.91 + ApplicationContainer serverApps = echoServer.Install (second.Get (1));
1.92 + serverApps.Start (Seconds (1.0));
1.93 + serverApps.Stop (Seconds (10.0));
1.94 +
1.95 + UdpEchoClientHelper echoClient (secondInterfaces.GetAddress (1), 9);
1.96 + echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
1.97 + echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.)));
1.98 + echoClient.SetAttribute ("PacketSize", UintegerValue (512));
1.99 +
1.100 + ApplicationContainer clientApps = echoClient.Install (first.Get (0));
1.101 + clientApps.Start (Seconds (2.0));
1.102 + clientApps.Stop (Seconds (10.0));
1.103 +
1.104 + OnOffHelper onOff ( "ns3::TcpSocketFactory",
1.105 + Address (InetSocketAddress ("192.168.1.1", port)));
1.106 + onOff.SetAttribute ("MaxBytes", UintegerValue (512));
1.107 +
1.108 + ApplicationContainer onOffApp = onOff.Install (second.Get (1));
1.109 +
1.110 + PacketSinkHelper packetSink ( "ns3::TcpSocketFactory",
1.111 + Address (InetSocketAddress ("192.168.1.1", port)));
1.112 +
1.113 + ApplicationContainer packetSinkApp = packetSink.Install (first.Get (0));
1.114 +
1.115 + /*packetSinkApp.Start (Seconds (2.0));
1.116 + packetSinkApp.Stop (Seconds (4.0));
1.117 +
1.118 + onOffApp.Start (Seconds (3.0));
1.119 + onOffApp.Stop (Seconds (4.0));*/
1.120 +
1.121 + /*ApplicationContainer pingApp = ping.Install (first.Get (0));
1.122 + pingApp.Start (Seconds (3.0));
1.123 + pingApp.Stop (Seconds (4.0));*/
1.124 +
1.125 + GlobalRouteManager::PopulateRoutingTables ();
1.126 +
1.127 + PointToPointHelper::EnablePcapAll ("netfilter");
1.128 +
1.129 + Simulator::Run ();
1.130 + Simulator::Destroy ();
1.131 + return 0;
1.132 +}