examples/socket/socket-bound-static-routing.cc
author Tom Henderson <tomh@tomh.org>
Fri, 20 Aug 2010 12:17:19 -0700
changeset 6589 9c325569fb01
parent 6014 d251d4a44fde
permissions -rw-r--r--
Help waf to guess release versions of nsc and pybindgen
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 ("SocketBoundRoutingExample");
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
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
    48
void BindSock (Ptr<Socket> sock, Ptr<NetDevice> netdev);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    49
void srcSocketRecv (Ptr<Socket> socket);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    50
void dstSocketRecv (Ptr<Socket> socket);
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
int 
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    53
main (int argc, char *argv[])
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    54
{
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
  // 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
    57
  // 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
    58
  CommandLine cmd;
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    59
  cmd.Parse (argc, argv);
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
  Ptr<Node> nSrc = CreateObject<Node> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    62
  Ptr<Node> nDst = CreateObject<Node> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    63
  Ptr<Node> nRtr1 = CreateObject<Node> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    64
  Ptr<Node> nRtr2 = CreateObject<Node> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    65
  Ptr<Node> nDstRtr = CreateObject<Node> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    66
  
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    67
  NodeContainer c = NodeContainer (nSrc, nDst, nRtr1, nRtr2, nDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    68
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    69
  InternetStackHelper internet;
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    70
  internet.Install (c);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    71
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    72
  // Point-to-point links
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    73
  NodeContainer nSrcnRtr1 = NodeContainer (nSrc, nRtr1);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    74
  NodeContainer nSrcnRtr2 = NodeContainer (nSrc, nRtr2);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    75
  NodeContainer nRtr1nDstRtr = NodeContainer (nRtr1, nDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    76
  NodeContainer nRtr2nDstRtr = NodeContainer (nRtr2, nDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    77
  NodeContainer nDstRtrnDst = NodeContainer (nDstRtr, nDst);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    78
  
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    79
  // 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
    80
  PointToPointHelper p2p;
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    81
  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    82
  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    83
  NetDeviceContainer dSrcdRtr1 = p2p.Install (nSrcnRtr1);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    84
  NetDeviceContainer dSrcdRtr2 = p2p.Install (nSrcnRtr2);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    85
  NetDeviceContainer dRtr1dDstRtr = p2p.Install (nRtr1nDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    86
  NetDeviceContainer dRtr2dDstRtr = p2p.Install (nRtr2nDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    87
  NetDeviceContainer dDstRtrdDst = p2p.Install (nDstRtrnDst);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    88
  
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    89
  Ptr<NetDevice> SrcToRtr1=dSrcdRtr1.Get (0);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    90
  Ptr<NetDevice> SrcToRtr2=dSrcdRtr2.Get (0);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    91
  
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    92
  // Later, we add IP addresses.  
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    93
  Ipv4AddressHelper ipv4;
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    94
  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
    95
  Ipv4InterfaceContainer iSrciRtr1 = ipv4.Assign (dSrcdRtr1);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    96
  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
    97
  Ipv4InterfaceContainer iSrciRtr2 = ipv4.Assign (dSrcdRtr2);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
    98
  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
    99
  Ipv4InterfaceContainer iRtr1iDstRtr = ipv4.Assign (dRtr1dDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   100
  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
   101
  Ipv4InterfaceContainer iRtr2iDstRtr = ipv4.Assign (dRtr2dDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   102
  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
   103
  Ipv4InterfaceContainer iDstRtrDst = ipv4.Assign (dDstRtrdDst);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   104
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   105
  Ptr<Ipv4> ipv4Src = nSrc->GetObject<Ipv4> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   106
  Ptr<Ipv4> ipv4Rtr1 = nRtr1->GetObject<Ipv4> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   107
  Ptr<Ipv4> ipv4Rtr2 = nRtr2->GetObject<Ipv4> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   108
  Ptr<Ipv4> ipv4DstRtr = nDstRtr->GetObject<Ipv4> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   109
  Ptr<Ipv4> ipv4Dst = nDst->GetObject<Ipv4> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   110
  
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   111
  Ipv4StaticRoutingHelper ipv4RoutingHelper;
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   112
  Ptr<Ipv4StaticRouting> staticRoutingSrc = ipv4RoutingHelper.GetStaticRouting (ipv4Src);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   113
  Ptr<Ipv4StaticRouting> staticRoutingRtr1 = ipv4RoutingHelper.GetStaticRouting (ipv4Rtr1);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   114
  Ptr<Ipv4StaticRouting> staticRoutingRtr2 = ipv4RoutingHelper.GetStaticRouting (ipv4Rtr2);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   115
  Ptr<Ipv4StaticRouting> staticRoutingDstRtr = ipv4RoutingHelper.GetStaticRouting (ipv4DstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   116
  Ptr<Ipv4StaticRouting> staticRoutingDst = ipv4RoutingHelper.GetStaticRouting (ipv4Dst);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   117
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   118
  // Create static routes from Src to Dst
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   119
  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
   120
  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
   121
  
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   122
  // Two routes to same destination - setting separate metrics. 
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   123
  // 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
   124
  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
   125
  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
   126
  
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   127
  // 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
   128
  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
   129
  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
   130
  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
   131
  
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   132
  Ipv4InterfaceAddress ifInAddrSrc=ipv4Src->GetAddress (1,0);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   133
  
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   134
  // 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
   135
  // Taken from tcp-large-transfer example
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   136
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   137
  Ptr<Socket> srcSocket = Socket::CreateSocket (nSrc, TypeId::LookupByName ("ns3::UdpSocketFactory"));
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   138
  srcSocket->Bind ();
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   139
  srcSocket->SetRecvCallback (MakeCallback (&srcSocketRecv));
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
  Ptr<Socket> dstSocket = Socket::CreateSocket (nDst, TypeId::LookupByName ("ns3::UdpSocketFactory"));
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   142
  uint16_t dstport = 12345;
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   143
  Ipv4Address dstaddr ("10.20.1.2");
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   144
  InetSocketAddress dst = InetSocketAddress (dstaddr, dstport);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   145
  dstSocket->Bind (dst);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   146
  dstSocket->SetRecvCallback (MakeCallback (&dstSocketRecv));
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   147
  
6014
d251d4a44fde checkpoint
Craig Dowell <craigdo@ee.washington.edu>
parents: 6009
diff changeset
   148
  AsciiTraceHelper ascii;
d251d4a44fde checkpoint
Craig Dowell <craigdo@ee.washington.edu>
parents: 6009
diff changeset
   149
  p2p.EnableAsciiAll (ascii.CreateFileStream ("socket-bound-static-routing.tr"));
6009
e1b696a1ed28 redo pcap tracing
Craig Dowell <craigdo@ee.washington.edu>
parents: 5857
diff changeset
   150
  p2p.EnablePcapAll ("socket-bound-static-routing");
5857
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   151
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   152
  LogComponentEnableAll (LOG_PREFIX_TIME);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   153
  LogComponentEnable ("SocketBoundRoutingExample", LOG_LEVEL_INFO);
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
  // First packet as normal (goes via Rtr1)
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   156
  Simulator::Schedule (Seconds (0.1),&SendStuff, srcSocket, dstaddr, dstport);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   157
  // Second via Rtr1 explicitly
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   158
  Simulator::Schedule (Seconds (1.0),&BindSock, srcSocket, SrcToRtr1);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   159
  Simulator::Schedule (Seconds( 1.1),&SendStuff, srcSocket, dstaddr, dstport);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   160
  // Third via Rtr2 explicitly
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   161
  Simulator::Schedule (Seconds (2.0),&BindSock, srcSocket, SrcToRtr2);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   162
  Simulator::Schedule (Seconds (2.1),&SendStuff, srcSocket, dstaddr, dstport);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   163
  // Fourth again as normal (goes via Rtr1)
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   164
  Simulator::Schedule (Seconds (3.0),&BindSock, srcSocket, Ptr<NetDevice>(0));
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   165
  Simulator::Schedule (Seconds (3.1),&SendStuff, srcSocket, dstaddr, dstport);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   166
  // 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
   167
  // bind to a socket not existing on a node
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   168
  // 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
   169
  Simulator::Run ();
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   170
  Simulator::Destroy ();
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
  return 0;
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   173
}
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   174
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   175
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
   176
{
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   177
  Ptr<Packet> p = Create<Packet> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   178
  p->AddPaddingAtEnd (100);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   179
  sock->SendTo (p, 0, InetSocketAddress (dstaddr,port));
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   180
  return;
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   181
}
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   182
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   183
void BindSock(Ptr<Socket> sock, Ptr<NetDevice> netdev)
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   184
{
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   185
  sock->BindToNetDevice (netdev);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   186
  return;
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   187
}
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
void
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   190
srcSocketRecv (Ptr<Socket> socket)
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
  Address from;
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   193
  Ptr<Packet> packet = socket->RecvFrom (from);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   194
  packet->RemoveAllPacketTags ();
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   195
  packet->RemoveAllByteTags ();
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   196
  InetSocketAddress address = InetSocketAddress::ConvertFrom (from);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   197
  NS_LOG_INFO ("Source Received " << packet->GetSize () << " bytes from " << address.GetIpv4());
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   198
  if (socket->GetBoundNetDevice ())
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   199
    {
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   200
      NS_LOG_INFO ("Socket was bound");
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
    else 
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   203
    {
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   204
      NS_LOG_INFO ("Socket was not bound");
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   205
    }
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
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   208
void
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   209
dstSocketRecv (Ptr<Socket> socket)
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   210
{
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   211
  Address from;
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   212
  Ptr<Packet> packet = socket->RecvFrom (from);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   213
  packet->RemoveAllPacketTags ();
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   214
  packet->RemoveAllByteTags ();
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   215
  InetSocketAddress address = InetSocketAddress::ConvertFrom (from);
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   216
  NS_LOG_INFO ("Destination Received " << packet->GetSize () << " bytes from " << address.GetIpv4 ());
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   217
  NS_LOG_INFO ("Triggering packet back to source node's interface 1");
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   218
  SendStuff(socket, Ipv4Address ("10.1.1.1"), address.GetPort ());
da61e3319def examples for Socket::BindToNetDevice()
Antti M?kel? <zarhan@cc.hut.fi>
parents:
diff changeset
   219
}