examples/socket/socket-bound-tcp-static-routing.cc
author Antti Mäkelä <zarhan@cc.hut.fi>
Tue, 15 Dec 2009 21:07:22 -0800
changeset 5857 da61e3319def
child 6009 e1b696a1ed28
permissions -rw-r--r--
examples for Socket::BindToNetDevice()
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5857
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
     2
/*
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
     3
 * This program is free software; you can redistribute it and/or modify
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
     4
 * it under the terms of the GNU General Public License version 2 as
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
     5
 * published by the Free Software Foundation;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
     6
 *
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
     7
 * This program is distributed in the hope that it will be useful,
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
     8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
     9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    10
 * GNU General Public License for more details.
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    11
 *
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    12
 * You should have received a copy of the GNU General Public License
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    13
 * along with this program; if not, write to the Free Software
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    15
 *
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    16
 */
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    17
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    18
/* Test program for multi-interface host, static routing
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    19
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    20
         Destination host (10.20.1.2)
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    21
                 |
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    22
                 | 10.20.1.0/24
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    23
              DSTRTR 
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    24
  10.10.1.0/24 /   \  10.10.2.0/24
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    25
              /     \
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    26
           Rtr1    Rtr2
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    27
 10.1.1.0/24 |      | 10.1.2.0/24
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    28
             |      /
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    29
              \    /
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    30
             Source
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    31
*/
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    32
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    33
#include <iostream>
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    34
#include <fstream>
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    35
#include <string>
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    36
#include <cassert>
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    37
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    38
#include "ns3/core-module.h"
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    39
#include "ns3/simulator-module.h"
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    40
#include "ns3/node-module.h"
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    41
#include "ns3/helper-module.h"
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    42
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    43
using namespace ns3;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    44
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    45
NS_LOG_COMPONENT_DEFINE ("SocketBoundTcpRoutingExample");
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    46
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    47
static const uint32_t totalTxBytes = 20000;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    48
static uint32_t currentTxBytes = 0;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    49
static const uint32_t writeSize = 1040;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    50
uint8_t data[writeSize];
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    51
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    52
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    53
void StartFlow (Ptr<Socket>, Ipv4Address, uint16_t);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    54
void WriteUntilBufferFull (Ptr<Socket>, uint32_t);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    55
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    56
void SendStuff (Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    57
void BindSock (Ptr<Socket> sock, Ptr<NetDevice> netdev);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    58
void srcSocketRecv (Ptr<Socket> socket);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    59
void dstSocketRecv (Ptr<Socket> socket);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    60
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    61
int 
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    62
main (int argc, char *argv[])
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    63
{
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    64
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    65
  // Allow the user to override any of the defaults and the above
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    66
  // DefaultValue::Bind ()s at run-time, via command-line arguments
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    67
  CommandLine cmd;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    68
  cmd.Parse (argc, argv);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    69
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    70
  Ptr<Node> nSrc = CreateObject<Node> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    71
  Ptr<Node> nDst = CreateObject<Node> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    72
  Ptr<Node> nRtr1 = CreateObject<Node> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    73
  Ptr<Node> nRtr2 = CreateObject<Node> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    74
  Ptr<Node> nDstRtr = CreateObject<Node> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    75
  
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    76
  NodeContainer c = NodeContainer (nSrc, nDst, nRtr1, nRtr2, nDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    77
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    78
  InternetStackHelper internet;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    79
  internet.Install (c);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    80
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    81
  // Point-to-point links
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    82
  NodeContainer nSrcnRtr1 = NodeContainer (nSrc, nRtr1);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    83
  NodeContainer nSrcnRtr2 = NodeContainer (nSrc, nRtr2);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    84
  NodeContainer nRtr1nDstRtr = NodeContainer (nRtr1, nDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    85
  NodeContainer nRtr2nDstRtr = NodeContainer (nRtr2, nDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    86
  NodeContainer nDstRtrnDst = NodeContainer (nDstRtr, nDst);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    87
  
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    88
  // We create the channels first without any IP addressing information
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    89
  PointToPointHelper p2p;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    90
  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    91
  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    92
  NetDeviceContainer dSrcdRtr1 = p2p.Install (nSrcnRtr1);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    93
  NetDeviceContainer dSrcdRtr2 = p2p.Install (nSrcnRtr2);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    94
  NetDeviceContainer dRtr1dDstRtr = p2p.Install (nRtr1nDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    95
  NetDeviceContainer dRtr2dDstRtr = p2p.Install (nRtr2nDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    96
  NetDeviceContainer dDstRtrdDst = p2p.Install (nDstRtrnDst);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    97
  
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    98
  Ptr<NetDevice> SrcToRtr1=dSrcdRtr1.Get (0);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    99
  Ptr<NetDevice> SrcToRtr2=dSrcdRtr2.Get (0);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   100
  
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   101
  // Later, we add IP addresses.  
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   102
  Ipv4AddressHelper ipv4;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   103
  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   104
  Ipv4InterfaceContainer iSrciRtr1 = ipv4.Assign (dSrcdRtr1);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   105
  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   106
  Ipv4InterfaceContainer iSrciRtr2 = ipv4.Assign (dSrcdRtr2);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   107
  ipv4.SetBase ("10.10.1.0", "255.255.255.0");
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   108
  Ipv4InterfaceContainer iRtr1iDstRtr = ipv4.Assign (dRtr1dDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   109
  ipv4.SetBase ("10.10.2.0", "255.255.255.0");
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   110
  Ipv4InterfaceContainer iRtr2iDstRtr = ipv4.Assign (dRtr2dDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   111
  ipv4.SetBase ("10.20.1.0", "255.255.255.0");
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   112
  Ipv4InterfaceContainer iDstRtrDst = ipv4.Assign (dDstRtrdDst);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   113
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   114
  Ptr<Ipv4> ipv4Src = nSrc->GetObject<Ipv4> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   115
  Ptr<Ipv4> ipv4Rtr1 = nRtr1->GetObject<Ipv4> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   116
  Ptr<Ipv4> ipv4Rtr2 = nRtr2->GetObject<Ipv4> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   117
  Ptr<Ipv4> ipv4DstRtr = nDstRtr->GetObject<Ipv4> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   118
  Ptr<Ipv4> ipv4Dst = nDst->GetObject<Ipv4> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   119
  
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   120
  Ipv4StaticRoutingHelper ipv4RoutingHelper;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   121
  Ptr<Ipv4StaticRouting> staticRoutingSrc = ipv4RoutingHelper.GetStaticRouting (ipv4Src);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   122
  Ptr<Ipv4StaticRouting> staticRoutingRtr1 = ipv4RoutingHelper.GetStaticRouting (ipv4Rtr1);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   123
  Ptr<Ipv4StaticRouting> staticRoutingRtr2 = ipv4RoutingHelper.GetStaticRouting (ipv4Rtr2);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   124
  Ptr<Ipv4StaticRouting> staticRoutingDstRtr = ipv4RoutingHelper.GetStaticRouting (ipv4DstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   125
  Ptr<Ipv4StaticRouting> staticRoutingDst = ipv4RoutingHelper.GetStaticRouting (ipv4Dst);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   126
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   127
  // Create static routes from Src to Dst
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   128
  staticRoutingRtr1->AddHostRouteTo (Ipv4Address ("10.20.1.2"), Ipv4Address ("10.10.1.2"), 2);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   129
  staticRoutingRtr2->AddHostRouteTo (Ipv4Address ("10.20.1.2"), Ipv4Address ("10.10.2.2"), 2);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   130
  
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   131
  // Two routes to same destination - setting separate metrics. 
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   132
  // You can switch these to see how traffic gets diverted via different routes
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   133
  staticRoutingSrc->AddHostRouteTo (Ipv4Address ("10.20.1.2"), Ipv4Address ("10.1.1.2"), 1,5);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   134
  staticRoutingSrc->AddHostRouteTo (Ipv4Address ("10.20.1.2"), Ipv4Address ("10.1.2.2"), 2,10);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   135
  
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   136
  // Creating static routes from DST to Source pointing to Rtr1 VIA Rtr2(!)
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   137
  staticRoutingDst->AddHostRouteTo (Ipv4Address ("10.1.1.1"), Ipv4Address ("10.20.1.1"), 1);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   138
  staticRoutingDstRtr->AddHostRouteTo (Ipv4Address ("10.1.1.1"), Ipv4Address ("10.10.2.1"), 2);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   139
  staticRoutingRtr2->AddHostRouteTo (Ipv4Address ("10.1.1.1"), Ipv4Address ("10.1.2.1"), 1);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   140
  
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   141
  staticRoutingDst->AddHostRouteTo (Ipv4Address ("10.1.2.1"), Ipv4Address ("10.20.1.1"), 1);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   142
  staticRoutingDstRtr->AddHostRouteTo (Ipv4Address ("10.1.2.1"), Ipv4Address ("10.10.2.1"), 2);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   143
  staticRoutingRtr2->AddHostRouteTo (Ipv4Address ("10.1.2.1"), Ipv4Address ("10.1.2.1"), 1);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   144
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   145
  Ipv4InterfaceAddress ifInAddrSrc=ipv4Src->GetAddress (1,0);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   146
  
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   147
  // There are no apps that can utilize the Socket Option so doing the work directly..
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   148
  // Taken from tcp-large-transfer example
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   149
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   150
  Ptr<Socket> srcSocket1 = Socket::CreateSocket (nSrc, TypeId::LookupByName ("ns3::TcpSocketFactory"));
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   151
  Ptr<Socket> srcSocket2 = Socket::CreateSocket (nSrc, TypeId::LookupByName ("ns3::TcpSocketFactory"));
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   152
  Ptr<Socket> srcSocket3 = Socket::CreateSocket (nSrc, TypeId::LookupByName ("ns3::TcpSocketFactory"));
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   153
  Ptr<Socket> srcSocket4 = Socket::CreateSocket (nSrc, TypeId::LookupByName ("ns3::TcpSocketFactory"));
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   154
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   155
  
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   156
  uint16_t dstport = 12345;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   157
  Ipv4Address dstaddr ("10.20.1.2");
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   158
  
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   159
  PacketSinkHelper sink ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny(), dstport));
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   160
  ApplicationContainer apps = sink.Install (nDst);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   161
  apps.Start (Seconds (0.0));
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   162
  apps.Stop (Seconds (10.0));
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   163
  
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   164
  std::ofstream ascii;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   165
  ascii.open ("socket-bound-tcp-static-routing.tr");
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   166
  PointToPointHelper::EnablePcapAll ("socket-bound-tcp-static-routing");
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   167
  PointToPointHelper::EnableAsciiAll (ascii);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   168
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   169
  LogComponentEnableAll (LOG_PREFIX_TIME);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   170
  LogComponentEnable ("SocketBoundTcpRoutingExample", LOG_LEVEL_INFO);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   171
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   172
  // First packet as normal (goes via Rtr1)
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   173
  Simulator::Schedule (Seconds (0.1),&StartFlow, srcSocket1, dstaddr, dstport);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   174
  // Second via Rtr1 explicitly
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   175
  Simulator::Schedule (Seconds (1.0),&BindSock, srcSocket2, SrcToRtr1);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   176
  Simulator::Schedule (Seconds (1.1),&StartFlow, srcSocket2, dstaddr, dstport);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   177
  // Third via Rtr2 explicitly
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   178
  Simulator::Schedule (Seconds (2.0),&BindSock, srcSocket3, SrcToRtr2);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   179
  Simulator::Schedule (Seconds (2.1),&StartFlow, srcSocket3, dstaddr, dstport);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   180
  // Fourth again as normal (goes via Rtr1)
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   181
  Simulator::Schedule (Seconds (3.0),&BindSock, srcSocket4, Ptr<NetDevice>(0));
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   182
  Simulator::Schedule (Seconds (3.1),&StartFlow, srcSocket4, dstaddr, dstport);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   183
  // If you uncomment what's below, it results in ASSERT failing since you can't 
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   184
  // bind to a socket not existing on a node
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   185
  // Simulator::Schedule(Seconds(4.0),&BindSock, srcSocket, dDstRtrdDst.Get(0)); 
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   186
  Simulator::Run ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   187
  Simulator::Destroy ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   188
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   189
  return 0;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   190
}
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   191
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   192
void BindSock(Ptr<Socket> sock, Ptr<NetDevice> netdev)
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   193
{
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   194
  sock->BindToNetDevice(netdev);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   195
  return;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   196
}
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   197
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   198
void StartFlow(Ptr<Socket> localSocket,
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   199
               Ipv4Address servAddress,
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   200
               uint16_t servPort)
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   201
{
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   202
  NS_LOG_INFO ("Starting flow at time " <<  Simulator::Now ().GetSeconds ());
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   203
  currentTxBytes = 0;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   204
  localSocket->Bind ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   205
  localSocket->Connect (InetSocketAddress (servAddress, servPort));//connect
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   206
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   207
  // tell the tcp implementation to call WriteUntilBufferFull again
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   208
  // if we blocked and new tx buffer space becomes available
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   209
  localSocket->SetSendCallback (MakeCallback (&WriteUntilBufferFull));
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   210
  WriteUntilBufferFull (localSocket, localSocket->GetTxAvailable ());
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   211
}
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   212
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   213
void WriteUntilBufferFull (Ptr<Socket> localSocket, uint32_t txSpace)
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   214
{
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   215
  while (currentTxBytes < totalTxBytes && localSocket->GetTxAvailable () > 0)
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   216
    {
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   217
      uint32_t left = totalTxBytes - currentTxBytes;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   218
      uint32_t dataOffset = currentTxBytes % writeSize;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   219
      uint32_t toWrite = writeSize - dataOffset;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   220
      toWrite = std::min (toWrite, left);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   221
      toWrite = std::min (toWrite, localSocket->GetTxAvailable ());
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   222
      int amountSent = localSocket->Send (&data[dataOffset], toWrite, 0);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   223
      if(amountSent < 0)
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   224
        {
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   225
          // we will be called again when new tx space becomes available.
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   226
          return;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   227
        }
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   228
      currentTxBytes += amountSent;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   229
    }
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   230
  localSocket->Close ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   231
}