examples/socket/socket-bound-static-routing.cc
author Ovidiu Poncea <ovidiu.poncea@cs.pub.ro>
Mon, 31 Aug 2015 10:41:05 -0700
changeset 11619 0b8c6e883208
parent 7256 b04ba6772f8c
permissions -rw-r--r--
bug 2158: Fix flooding of packets on input port, for packets received on in!=0 When you have the action OFPP_FLOOD on a flow, and the received packet is not coming into the switch through port 0, then the packet will be flooded on all ports, including the receiving port! This is especially bad if you have broadcasts as packets will get replicated indefinitely (ARP req for e.g.). This is caused by missing ntohs() that fails to convert port numbers to their correct values. The switch works correctly for broadcast packets received on port zero. Example: The ReceivePacketOut() gets 0 if the input port is 0 and it works. But will get 512 if the input port is 1 and since 512 is not our real input port the check will fail and the packet will also be replicated on the input port.
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
7196
0f12b1572bca general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6865
diff changeset
    25
              / \
5857
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"
6823
a27f86fb4e55 Merge node and common modules into new network module
Tom Henderson <tomh@tomh.org>
parents: 6821
diff changeset
    39
#include "ns3/network-module.h"
6848
1f453ad50ef3 Converts csma, emu, tap-bridge, point-to-point, wifi and wimax modules into modular format
Lalith Suresh <suresh.lalith@gmail.com>
parents: 6845
diff changeset
    40
#include "ns3/internet-module.h"
1f453ad50ef3 Converts csma, emu, tap-bridge, point-to-point, wifi and wimax modules into modular format
Lalith Suresh <suresh.lalith@gmail.com>
parents: 6845
diff changeset
    41
#include "ns3/point-to-point-module.h"
6648
d1785ce489c5 Module layout rework for static-routing, list-routing, global-routing
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6014
diff changeset
    42
#include "ns3/ipv4-static-routing-helper.h"
d1785ce489c5 Module layout rework for static-routing, list-routing, global-routing
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6014
diff changeset
    43
#include "ns3/ipv4-list-routing-helper.h"
5857
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
using namespace ns3;
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
NS_LOG_COMPONENT_DEFINE ("SocketBoundRoutingExample");
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    48
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    49
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
    50
void BindSock (Ptr<Socket> sock, Ptr<NetDevice> netdev);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    51
void srcSocketRecv (Ptr<Socket> socket);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    52
void dstSocketRecv (Ptr<Socket> socket);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    53
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    54
int 
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    55
main (int argc, char *argv[])
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    56
{
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    57
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    58
  // 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
    59
  // 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
    60
  CommandLine cmd;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    61
  cmd.Parse (argc, argv);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    62
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    63
  Ptr<Node> nSrc = CreateObject<Node> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    64
  Ptr<Node> nDst = CreateObject<Node> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    65
  Ptr<Node> nRtr1 = CreateObject<Node> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    66
  Ptr<Node> nRtr2 = CreateObject<Node> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    67
  Ptr<Node> nDstRtr = CreateObject<Node> ();
7196
0f12b1572bca general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6865
diff changeset
    68
5857
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    69
  NodeContainer c = NodeContainer (nSrc, nDst, nRtr1, nRtr2, nDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    70
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    71
  InternetStackHelper internet;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    72
  internet.Install (c);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    73
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    74
  // Point-to-point links
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    75
  NodeContainer nSrcnRtr1 = NodeContainer (nSrc, nRtr1);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    76
  NodeContainer nSrcnRtr2 = NodeContainer (nSrc, nRtr2);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    77
  NodeContainer nRtr1nDstRtr = NodeContainer (nRtr1, nDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    78
  NodeContainer nRtr2nDstRtr = NodeContainer (nRtr2, nDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    79
  NodeContainer nDstRtrnDst = NodeContainer (nDstRtr, nDst);
7196
0f12b1572bca general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6865
diff changeset
    80
5857
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    81
  // 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
    82
  PointToPointHelper p2p;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    83
  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    84
  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    85
  NetDeviceContainer dSrcdRtr1 = p2p.Install (nSrcnRtr1);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    86
  NetDeviceContainer dSrcdRtr2 = p2p.Install (nSrcnRtr2);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    87
  NetDeviceContainer dRtr1dDstRtr = p2p.Install (nRtr1nDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    88
  NetDeviceContainer dRtr2dDstRtr = p2p.Install (nRtr2nDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    89
  NetDeviceContainer dDstRtrdDst = p2p.Install (nDstRtrnDst);
7196
0f12b1572bca general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6865
diff changeset
    90
5857
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    91
  Ptr<NetDevice> SrcToRtr1=dSrcdRtr1.Get (0);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    92
  Ptr<NetDevice> SrcToRtr2=dSrcdRtr2.Get (0);
7196
0f12b1572bca general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6865
diff changeset
    93
0f12b1572bca general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6865
diff changeset
    94
  // Later, we add IP addresses.
5857
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    95
  Ipv4AddressHelper ipv4;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    96
  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
    97
  Ipv4InterfaceContainer iSrciRtr1 = ipv4.Assign (dSrcdRtr1);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
    98
  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
    99
  Ipv4InterfaceContainer iSrciRtr2 = ipv4.Assign (dSrcdRtr2);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   100
  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
   101
  Ipv4InterfaceContainer iRtr1iDstRtr = ipv4.Assign (dRtr1dDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   102
  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
   103
  Ipv4InterfaceContainer iRtr2iDstRtr = ipv4.Assign (dRtr2dDstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   104
  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
   105
  Ipv4InterfaceContainer iDstRtrDst = ipv4.Assign (dDstRtrdDst);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   106
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   107
  Ptr<Ipv4> ipv4Src = nSrc->GetObject<Ipv4> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   108
  Ptr<Ipv4> ipv4Rtr1 = nRtr1->GetObject<Ipv4> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   109
  Ptr<Ipv4> ipv4Rtr2 = nRtr2->GetObject<Ipv4> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   110
  Ptr<Ipv4> ipv4DstRtr = nDstRtr->GetObject<Ipv4> ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   111
  Ptr<Ipv4> ipv4Dst = nDst->GetObject<Ipv4> ();
7196
0f12b1572bca general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6865
diff changeset
   112
5857
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   113
  Ipv4StaticRoutingHelper ipv4RoutingHelper;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   114
  Ptr<Ipv4StaticRouting> staticRoutingSrc = ipv4RoutingHelper.GetStaticRouting (ipv4Src);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   115
  Ptr<Ipv4StaticRouting> staticRoutingRtr1 = ipv4RoutingHelper.GetStaticRouting (ipv4Rtr1);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   116
  Ptr<Ipv4StaticRouting> staticRoutingRtr2 = ipv4RoutingHelper.GetStaticRouting (ipv4Rtr2);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   117
  Ptr<Ipv4StaticRouting> staticRoutingDstRtr = ipv4RoutingHelper.GetStaticRouting (ipv4DstRtr);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   118
  Ptr<Ipv4StaticRouting> staticRoutingDst = ipv4RoutingHelper.GetStaticRouting (ipv4Dst);
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
  // Create static routes from Src to Dst
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   121
  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
   122
  staticRoutingRtr2->AddHostRouteTo (Ipv4Address ("10.20.1.2"), Ipv4Address ("10.10.2.2"), 2);
7196
0f12b1572bca general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6865
diff changeset
   123
5857
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   124
  // Two routes to same destination - setting separate metrics. 
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   125
  // 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
   126
  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
   127
  staticRoutingSrc->AddHostRouteTo (Ipv4Address ("10.20.1.2"), Ipv4Address ("10.1.2.2"), 2,10);
7196
0f12b1572bca general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6865
diff changeset
   128
5857
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   129
  // 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
   130
  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
   131
  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
   132
  staticRoutingRtr2->AddHostRouteTo (Ipv4Address ("10.1.1.1"), Ipv4Address ("10.1.2.1"), 1);
7196
0f12b1572bca general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6865
diff changeset
   133
5857
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));
7196
0f12b1572bca general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6865
diff changeset
   140
5857
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));
7196
0f12b1572bca general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6865
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);
7256
b04ba6772f8c rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents: 7217
diff changeset
   159
  Simulator::Schedule (Seconds ( 1.1),&SendStuff, srcSocket, dstaddr, dstport);
5857
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
7256
b04ba6772f8c rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents: 7217
diff changeset
   175
void SendStuff (Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port)
5857
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
7256
b04ba6772f8c rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents: 7217
diff changeset
   183
void BindSock (Ptr<Socket> sock, Ptr<NetDevice> netdev)
5857
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 ();
7256
b04ba6772f8c rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents: 7217
diff changeset
   196
  NS_LOG_INFO ("Source Received " << packet->GetSize () << " bytes from " << InetSocketAddress::ConvertFrom (from).GetIpv4 ());
5857
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   197
  if (socket->GetBoundNetDevice ())
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   198
    {
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   199
      NS_LOG_INFO ("Socket was bound");
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   200
    } 
7196
0f12b1572bca general examples coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6865
diff changeset
   201
  else
5857
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   202
    {
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   203
      NS_LOG_INFO ("Socket was not bound");
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   204
    }
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
void
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   208
dstSocketRecv (Ptr<Socket> socket)
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   209
{
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   210
  Address from;
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   211
  Ptr<Packet> packet = socket->RecvFrom (from);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   212
  packet->RemoveAllPacketTags ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   213
  packet->RemoveAllByteTags ();
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   214
  InetSocketAddress address = InetSocketAddress::ConvertFrom (from);
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   215
  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
   216
  NS_LOG_INFO ("Triggering packet back to source node's interface 1");
7256
b04ba6772f8c rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents: 7217
diff changeset
   217
  SendStuff (socket, Ipv4Address ("10.1.1.1"), address.GetPort ());
5857
da61e3319def examples for Socket::BindToNetDevice()
Antti Mäkelä <zarhan@cc.hut.fi>
parents:
diff changeset
   218
}