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 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/src/internet-stack/nat-rule.cc Thu Aug 06 11:16:51 2009 +0600
2.3 @@ -0,0 +1,59 @@
2.4 +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2.5 +/*
2.6 + * Copyright (c) 2009 University of Texas at Dallas
2.7 + *
2.8 + * This program is free software; you can redistribute it and/or modify
2.9 + * it under the terms of the GNU General Public License version 2 as
2.10 + * published by the Free Software Foundation;
2.11 + *
2.12 + * This program is distributed in the hope that it will be useful,
2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2.15 + * GNU General Public License for more details.
2.16 + *
2.17 + * You should have received a copy of the GNU General Public License
2.18 + * along with this program; if not, write to the Free Software
2.19 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2.20 + *
2.21 + * Author: Qasim Javed <qasim@utdallas.edu>
2.22 + */
2.23 +#include "nat-rule.h"
2.24 +
2.25 +namespace ns3 {
2.26 +
2.27 +NatRule::NatRule (Ipv4Address orig, Ipv4Address mapped, Ptr<NetDevice> device) //, Ipv4Interface outInterface
2.28 +{
2.29 + m_originalSource = orig;
2.30 + m_mangledSource = mapped;
2.31 + m_device = device;
2.32 + //m_outInterface = outInterface;
2.33 +}
2.34 +
2.35 +bool
2.36 +NatRule::operator == (const NatRule& other) const
2.37 +{
2.38 + return (m_originalSource == other.m_originalSource) &&
2.39 + (m_mangledSource == other.m_mangledSource) &&
2.40 + (m_device == other.m_device);
2.41 + // (m_outInterface == other.m_outInterface);
2.42 +}
2.43 +
2.44 +Ipv4Address
2.45 +NatRule::GetOriginalSource () const
2.46 +{
2.47 + return m_originalSource;
2.48 +}
2.49 +
2.50 +Ipv4Address
2.51 +NatRule::GetMangledSource () const
2.52 +{
2.53 + return m_mangledSource;
2.54 +}
2.55 +
2.56 +Ptr<NetDevice>
2.57 +NatRule::GetDevice () const
2.58 +{
2.59 + return m_device;
2.60 +}
2.61 +
2.62 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/src/internet-stack/nat-rule.h Thu Aug 06 11:16:51 2009 +0600
3.3 @@ -0,0 +1,57 @@
3.4 +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3.5 +/*
3.6 + * Copyright (c) 2009 University of Texas at Dallas
3.7 + *
3.8 + * This program is free software; you can redistribute it and/or modify
3.9 + * it under the terms of the GNU General Public License version 2 as
3.10 + * published by the Free Software Foundation;
3.11 + *
3.12 + * This program is distributed in the hope that it will be useful,
3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3.15 + * GNU General Public License for more details.
3.16 + *
3.17 + * You should have received a copy of the GNU General Public License
3.18 + * along with this program; if not, write to the Free Software
3.19 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3.20 + *
3.21 + * Author: Qasim Javed <qasim@utdallas.edu>
3.22 + */
3.23 +#ifndef NAT_RULE_H
3.24 +#define NAT_RULE_H
3.25 +
3.26 +#include "ns3/log.h"
3.27 +#include "ns3/ipv4-address.h"
3.28 +#include "ns3/ref-count-base.h"
3.29 +#include "ns3/ptr.h"
3.30 +#include "ns3/object.h"
3.31 +#include "ns3/callback.h"
3.32 +#include "ns3/net-device.h"
3.33 +//#include "ipv4-interface.h"
3.34 +//#include "jhash.h"
3.35 +
3.36 +
3.37 +namespace ns3 {
3.38 +
3.39 +class Object;
3.40 +class Ipv4Address;
3.41 +class NetDevice;
3.42 +
3.43 +class NatRule : public RefCountBase {
3.44 + public:
3.45 + NatRule (Ipv4Address orig, Ipv4Address mapped, Ptr<NetDevice> device); //, Ipv4Interface outInterface);
3.46 + bool operator == (const NatRule& other) const;
3.47 + Ipv4Address GetOriginalSource () const;
3.48 +
3.49 + Ipv4Address GetMangledSource () const;
3.50 + Ptr <NetDevice> GetDevice () const;
3.51 +
3.52 + private:
3.53 + Ipv4Address m_originalSource;
3.54 + Ipv4Address m_mangledSource;
3.55 + Ptr <NetDevice> m_device;
3.56 + //Ipv4Interface m_outInterface;
3.57 +};
3.58 +
3.59 +}
3.60 +#endif /* NAT_RULE_H */