Printing Routing Tables to stdout

3,117 views
Skip to first unread message

Luiz Felipe Z. Saggioro

unread,
Feb 7, 2012, 10:09:31 PM2/7/12
to ns-3-users
Im having trouble to print the routing tables to stdout. Im using the
following code:

Ptr<OutputStreamWrapper> stream ("routingtable", "w");
Ipv4GlobalRoutingHelper::PrintRoutingTableAllAt (Seconds (0.5),
stream);

but its complaining about the OutputStreamWrapper. Is there any other
way to do this task?

Thanks in advance.

Tom Henderson

unread,
Feb 8, 2012, 10:13:13 AM2/8/12
to ns-3-...@googlegroups.com

See the examples/routing/dynamic-global-routing.cc file for a working
example. This is a pattern of the usage (note that you should use
Create<> to instantiate the OutputStreamWrapper, and that
PrintRoutingTableAllAt is not a static method):

// Trace routing tables
Ipv4GlobalRoutingHelper g;
Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper>
("dynamic-global-routing.routes", std::ios::out);
g.PrintRoutingTableAllAt (Seconds (12), routingStream);

Luiz Felipe Z. Saggioro

unread,
Feb 8, 2012, 10:27:22 AM2/8/12
to ns-3-...@googlegroups.com
Thanks a lot for the quick answer, Tom! You helped me a lot.

Yeison Camargo

unread,
Feb 9, 2012, 11:15:57 AM2/9/12
to ns-3-...@googlegroups.com
Hi, Luiz Felipe,

Did you make it work as cout ??? Because i was working with the routing table but all what i could do it to create a file where the routing table is saved but not in the console output (like when you print cout<<"Hello world";), This is the script i was working but just could make it work in files (The script is based on a ns-3 example).

// Test program for this 3-router scenario, using static routing
//
// (a.a.a.a/32)A<--x.x.x.0/30-->B<--y.y.y.0/30-->C(c.c.c.c/32)

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/csma-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/gtk-config-store.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE("staticRouting");

int
main (int argc, char** argv)
{
  LogComponentEnable("staticRouting",LOG_LEVEL_INFO);

  CommandLine cmd;
  cmd.Parse(argc, argv);

  Ptr<Node> nA = CreateObject<Node>();
  Ptr<Node> nB = CreateObject<Node>();
  Ptr<Node> nC = CreateObject<Node>();

  NodeContainer c = NodeContainer (nA, nB, nC);

  InternetStackHelper stack;
  stack.Install(c);

  // Point-to-point links
  NodeContainer nAnB = NodeContainer (nA, nB);
  NodeContainer nBnC = NodeContainer (nB, nC);

  // We create the channels first without any IP addressing information

  PointToPointHelper p2p;
  p2p.SetChannelAttribute("Delay", StringValue ("2ms"));
  p2p.SetDeviceAttribute("DataRate", StringValue ("5Mbps"));

  NetDeviceContainer dAdB = p2p.Install(nAnB);
  NetDeviceContainer dBdC = p2p.Install(nBnC);

  Ptr<CsmaNetDevice> deviceA = CreateObject<CsmaNetDevice>();
  deviceA->SetAddress(Mac48Address::Allocate());
  nA->AddDevice(deviceA);

  Ptr<CsmaNetDevice> deviceC = CreateObject<CsmaNetDevice>();
  deviceC->SetAddress(Mac48Address::Allocate());
  nC->AddDevice(deviceC);

  // Later, we add IP addresses.
  Ipv4AddressHelper ipv4;
  ipv4.SetBase("10.1.1.0", "255.255.255.252");
  Ipv4InterfaceContainer iAiB = ipv4.Assign(dAdB);

  ipv4.SetBase("10.1.1.4", "255.255.255.252");
  Ipv4InterfaceContainer iBiC = ipv4.Assign(dBdC);

  Ptr<Ipv4> ipv4A = nA->GetObject<Ipv4>();
  Ptr<Ipv4> ipv4B = nB->GetObject<Ipv4>();
  Ptr<Ipv4> ipv4C = nC->GetObject<Ipv4>();

  int32_t ifIndexA = ipv4A->AddInterface(deviceA);
  int32_t ifIndexC = ipv4C->AddInterface(deviceC);

  Ipv4InterfaceAddress ifInAddrA = Ipv4InterfaceAddress (Ipv4Address ("172.16.1.1"), Ipv4Mask ("/32"));
  ipv4A->AddAddress(ifIndexA, ifInAddrA);
  ipv4A->SetMetric(ifIndexA, 1);
  ipv4A->SetUp(ifIndexA);

  Ipv4InterfaceAddress ifInAddrC = Ipv4InterfaceAddress (Ipv4Address ("192.168.1.1"), Ipv4Mask("/32"));
  ipv4C->AddAddress(ifIndexC, ifInAddrC);
  ipv4C->SetMetric(ifIndexC, 1);
  ipv4C->SetUp(ifIndexC);

  Ipv4StaticRoutingHelper ipv4RoutingHelper;
  // Create static routes from A to C
  Ptr<Ipv4StaticRouting> staticRoutingA = ipv4RoutingHelper.GetStaticRouting(ipv4A);
  // The ifIndex for this outbound route is 1; the first p2p link added
  staticRoutingA->AddHostRouteTo(Ipv4Address ("192.168.1.1"), Ipv4Address ("10.1.1.2"), 1); // 1 is the interface

  Ptr<Ipv4StaticRouting> staticRoutingB = ipv4RoutingHelper.GetStaticRouting(ipv4B);
  // The ifIndex we want on node B is 2; 0 corresponds to loopback, and 1 to the first point to point link
  staticRoutingB->AddHostRouteTo(Ipv4Address ("192.168.1.1"), Ipv4Address ("10.1.1.6"), 2); //2 is the interface


  //Print Routin Table
  Ptr<OutputStreamWrapper> routintable = Create<OutputStreamWrapper>("routingtable",std::ios::out);
  ipv4RoutingHelper.PrintRoutingTableAt(Seconds(0), nA, routintable);
  ipv4RoutingHelper.PrintRoutingTableAt(Seconds(0), nB, routintable);
  ipv4RoutingHelper.PrintRoutingTableAt(Seconds(0), nC, routintable);

  Ptr<OutputStreamWrapper> allroutingtable = Create<OutputStreamWrapper>("routingtableallnodes",std::ios::out);
  ipv4RoutingHelper.PrintRoutingTableAllAt(Seconds(0), allroutingtable);

  Ptr<OutputStreamWrapper> routingtableevery = Create<OutputStreamWrapper>("routingtableevery",std::ios::out);
  ipv4RoutingHelper.PrintRoutingTableEvery(Seconds(2), nA, routingtableevery);

  Ptr<OutputStreamWrapper> routingtableallevery = Create<OutputStreamWrapper>("routingtableallevery",std::ios::out);
  ipv4RoutingHelper.PrintRoutingTableAllEvery(Seconds(2), routingtableallevery);

  Ptr<OutputStreamWrapper> testprint = Create<OutputStreamWrapper>("routingtestprint", std::ios::out);
  staticRoutingA->PrintRoutingTable(testprint);

//  Ptr<OutputStreamWrapper> testprint = Create<OutputStreamWrapper>(std::cout);
//  staticRoutingA->PrintRoutingTable(testprint);
//  std::cout << *testprint->GetStream();
//  NS_LOG_INFO(*testprint->GetStream());

  // Create the OnOff application to send UDP datagrams of size
  // 210 bytes at a rate of 448 Kb/s from A->C

  uint16_t port = 9;   // Discard port (RFC 863)

  OnOffHelper onOff ("ns3::UdpSocketFactory", Address (InetSocketAddress (ifInAddrC.GetLocal(), port)));
  onOff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1)));
  onOff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
  onOff.SetAttribute ("DataRate", DataRateValue (DataRate (6000)));

  ApplicationContainer apps = onOff.Install(nA);
  apps.Start(Seconds(1.0));
  apps.Stop(Seconds(10.0));

  // Create a packet sink to receive these packets
  PacketSinkHelper sink ("ns3::UdpSocketFactory", Address (InetSocketAddress (Ipv4Address::GetAny(), port))); //why Ipv4Address::GetAny() ??
  apps = sink.Install(nC);
  apps.Start(Seconds(1.0));
  apps.Stop(Seconds(10.0));

  p2p.EnablePcapAll("staticrouting");

  NS_LOG_INFO("Run");

//  GtkConfigStore config;
//  config.ConfigureDefaults();
//  config.ConfigureAttributes();

  Simulator::Stop(Seconds(11));
  Simulator::Run();
  Simulator::Destroy();

}

On Wed, Feb 8, 2012 at 10:27 AM, Luiz Felipe Z. Saggioro <luizfzs...@gmail.com> wrote:
Thanks a lot for the quick answer, Tom! You helped me a lot.

--
You received this message because you are subscribed to the Google Groups "ns-3-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/ns-3-users/-/ADuUvTuHEEAJ.

To post to this group, send email to ns-3-...@googlegroups.com.
To unsubscribe from this group, send email to ns-3-users+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ns-3-users?hl=en.



--
YEISON JULIAN CAMARGO
Cisco Systems CCNA
Cisco Systems CCNA Security
Microsoft MTA Windows Server Administration
Microsoft MTA Security
Microsoft MTA Networking
IPv6 Hurricane Electric SAGE

Reply all
Reply to author
Forward
0 new messages