samples/main-packet-printer.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Wed, 29 Aug 2007 14:43:31 +0200
changeset 1391 ce9ab2cbf936
parent 1141 a75fc1741700
child 1866 e7dbcc4df546
permissions -rw-r--r--
add some tracing documentation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
913
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
     2
/*
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
     3
 * Copyright (c) 2006,2007 INRIA
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
     4
 * All rights reserved.
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
     5
 *
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
     7
 * it under the terms of the GNU General Public License version 2 as
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
     8
 * published by the Free Software Foundation;
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
     9
 *
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    13
 * GNU General Public License for more details.
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    14
 *
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    15
 * You should have received a copy of the GNU General Public License
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    16
 * along with this program; if not, write to the Free Software
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    18
 *
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    19
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    20
 */
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    21
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    22
#include "ns3/packet.h"
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    23
#include "ns3/header.h"
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    24
#include "ns3/packet-printer.h"
901
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
    25
#include "ns3/ipv4-header.h"
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
    26
#include "ns3/udp-header.h"
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    27
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    28
using namespace ns3;
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    29
913
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    30
// This sample file shows how to use the Packet metadata facility
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    31
//
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    32
// Packets are stored as ``packed'' data structures, to facilitate
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    33
// fragmentation and network emulation.  However, when debugging a program,
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    34
// or for certain tracing applications, it may be convenient to dump out
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    35
// the contents of a packet header in a human-friendly form.
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    36
//
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    37
// To do this, a few things are needed:
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    38
// i) enable the metadata facility (disabled by default, because it causes
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    39
//    a small performance hit 
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    40
// ii) decide on whether you want to use a default or customized (you
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    41
//     provide your own) routine to dump a particular header
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    42
//
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    43
// This sample steps through two routines; one to use the default
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    44
// printing of IPv4 and UDP headers, and one to show a non-default case.
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    45
// There is a lot of emphasis in this sample of how this facility
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    46
// interacts with packet fragmentation.
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    47
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    48
void DefaultPrint (void)
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    49
{
893
9d50280ae588 comment the sample code a little bit
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 892
diff changeset
    50
  // We create a packet with 1000 bytes of zero payload
9d50280ae588 comment the sample code a little bit
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 892
diff changeset
    51
  // and add 3 headers to this packet.
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    52
  Packet p (1000);
901
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
    53
  Ipv4Header ipv4;
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
    54
  UdpHeader udp;
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
    55
  ipv4.SetSource (Ipv4Address ("192.168.0.1"));
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
    56
  ipv4.SetDestination (Ipv4Address ("192.168.0.2"));
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
    57
  udp.SetSource (1025);
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
    58
  udp.SetDestination (80);
914
a23c5c2fb713 Add payload size to UDP
tomh@ns-regression.ee.washington.edu
parents: 913
diff changeset
    59
  udp.SetPayloadSize (1000);
901
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
    60
  p.AddHeader (udp);
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
    61
  p.AddHeader (ipv4);
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    62
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    63
  std::cout << "full packet size=" << p.GetSize () << std::endl;
913
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
    64
  // Here, invoke the default Print routine, directed to std out
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    65
  p.Print (std::cout);
892
b3f73643c055 add separator support to PacketPrinter
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 891
diff changeset
    66
  std::cout << std::endl;
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    67
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    68
893
9d50280ae588 comment the sample code a little bit
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 892
diff changeset
    69
  // Now, we fragment our packet in 3 consecutive pieces.
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    70
  Packet p1 = p.CreateFragment (0, 2);
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    71
  Packet p2 = p.CreateFragment (2, 1000);
901
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
    72
  Packet p3 = p.CreateFragment (1002, 26);
893
9d50280ae588 comment the sample code a little bit
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 892
diff changeset
    73
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    74
  std::cout << "fragment1" << std::endl;
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    75
  p1.Print (std::cout);
892
b3f73643c055 add separator support to PacketPrinter
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 891
diff changeset
    76
  std::cout << std::endl;
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    77
  std::cout << "fragment2" << std::endl;
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    78
  p2.Print (std::cout);
892
b3f73643c055 add separator support to PacketPrinter
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 891
diff changeset
    79
  std::cout << std::endl;
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    80
  std::cout << "fragment3" << std::endl;
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    81
  p3.Print (std::cout);
892
b3f73643c055 add separator support to PacketPrinter
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 891
diff changeset
    82
  std::cout << std::endl;
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    83
893
9d50280ae588 comment the sample code a little bit
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 892
diff changeset
    84
  // And, finally, we re-aggregate the 3 consecutive pieces.
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    85
  Packet aggregate = p1;
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    86
  aggregate.AddAtEnd (p2);
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    87
  aggregate.AddAtEnd (p3);
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    88
  std::cout << "aggregated" << std::endl;
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    89
  aggregate.Print (std::cout);
892
b3f73643c055 add separator support to PacketPrinter
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 891
diff changeset
    90
  std::cout << std::endl;
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    91
}
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    92
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    93
void
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    94
DoPrintPayload (std::ostream & os,uint32_t packetUid,uint32_t size,
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    95
                struct PacketPrinter::FragmentInformation info)
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    96
{
901
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
    97
  os << "PAYLOAD (size " << size << " trim_start " << info.start << " trim_end " << info.end << ")";
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    98
}
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    99
void 
901
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
   100
DoPrintIpv4Header (std::ostream &os, uint32_t packetUid, uint32_t size, const Ipv4Header *ipv4)
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   101
{
901
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
   102
  os << "IPV4 " << ipv4->GetSource () << " > " << ipv4->GetDestination ();
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   103
}
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   104
void 
901
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
   105
DoPrintIpv4HeaderFragment (std::ostream &os, uint32_t packetUid, uint32_t size,
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
   106
                          std::string &name, struct PacketPrinter::FragmentInformation info)
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   107
{
901
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
   108
  os << "IPV4 fragment";
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   109
}
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   110
913
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
   111
// This function walks through a non-default case.  A few features of
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
   112
// the API (defined in common/packet-printer.h) are shown.
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
   113
//
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   114
void NonDefaultPrint (void)
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   115
{
893
9d50280ae588 comment the sample code a little bit
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 892
diff changeset
   116
  // create an adhoc packet printer.
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   117
  PacketPrinter printer;
893
9d50280ae588 comment the sample code a little bit
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 892
diff changeset
   118
  // print from first header to last trailer
892
b3f73643c055 add separator support to PacketPrinter
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 891
diff changeset
   119
  printer.PrintForward ();
893
9d50280ae588 comment the sample code a little bit
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 892
diff changeset
   120
  // set a string separator automatically inserted
9d50280ae588 comment the sample code a little bit
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 892
diff changeset
   121
  // between each call to a printing function.
892
b3f73643c055 add separator support to PacketPrinter
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 891
diff changeset
   122
  printer.SetSeparator (" - ");
893
9d50280ae588 comment the sample code a little bit
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 892
diff changeset
   123
  // set the payload print function
1141
a75fc1741700 rework the PacketPrinter API and implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 914
diff changeset
   124
  printer.SetPayloadPrinter (MakeCallback (&DoPrintPayload));
901
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
   125
  // set the print function for the header type Ipv4Header.
1141
a75fc1741700 rework the PacketPrinter API and implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 914
diff changeset
   126
  printer.SetHeaderPrinter (MakeCallback (&DoPrintIpv4Header),
901
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
   127
                            MakeCallback (&DoPrintIpv4HeaderFragment));
893
9d50280ae588 comment the sample code a little bit
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 892
diff changeset
   128
9d50280ae588 comment the sample code a little bit
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 892
diff changeset
   129
9d50280ae588 comment the sample code a little bit
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 892
diff changeset
   130
  // We create a packet with 1000 bytes of zero payload
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   131
  Packet p (1000);
901
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
   132
  Ipv4Header ipv4;
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
   133
  UdpHeader udp;
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
   134
  ipv4.SetSource (Ipv4Address ("192.168.0.1"));
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
   135
  ipv4.SetDestination (Ipv4Address ("192.168.0.2"));
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
   136
  udp.SetSource (1025);
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
   137
  udp.SetDestination (80);
914
a23c5c2fb713 Add payload size to UDP
tomh@ns-regression.ee.washington.edu
parents: 913
diff changeset
   138
  udp.SetPayloadSize (1000);
901
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
   139
  p.AddHeader (udp);
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
   140
  p.AddHeader (ipv4);
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   141
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   142
  std::cout << "full packet size=" << p.GetSize () << std::endl;
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   143
  p.Print (std::cout, printer);
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   144
  std::cout << std::endl;
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   145
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   146
893
9d50280ae588 comment the sample code a little bit
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 892
diff changeset
   147
  // fragment our packet in 3 pieces
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   148
  Packet p1 = p.CreateFragment (0, 2);
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   149
  Packet p2 = p.CreateFragment (2, 1000);
901
454dbce6dc4e modify sample packet printer to use ipv4 and udp headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 893
diff changeset
   150
  Packet p3 = p.CreateFragment (1002, 26);
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   151
  std::cout << "fragment1" << std::endl;
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   152
  p1.Print (std::cout, printer);
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   153
  std::cout << std::endl;
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   154
  std::cout << "fragment2" << std::endl;
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   155
  p2.Print (std::cout, printer);
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   156
  std::cout << std::endl;
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   157
  std::cout << "fragment3" << std::endl;
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   158
  p3.Print (std::cout, printer);
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   159
  std::cout << std::endl;
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   160
893
9d50280ae588 comment the sample code a little bit
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 892
diff changeset
   161
  // aggregate all 3 fragments of the original packet
9d50280ae588 comment the sample code a little bit
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 892
diff changeset
   162
  // to reconstruct a copy of the original packet.
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   163
  Packet aggregate = p1;
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   164
  aggregate.AddAtEnd (p2);
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   165
  aggregate.AddAtEnd (p3);
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   166
  std::cout << "aggregated" << std::endl;
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   167
  aggregate.Print (std::cout, printer);
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   168
  std::cout << std::endl;
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   169
}
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   170
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   171
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   172
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   173
int main (int argc, char *argv[])
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   174
{
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   175
  Packet::EnableMetadata ();
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   176
913
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
   177
  std::cout << "DefaultPrint()" << std::endl;
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   178
  DefaultPrint ();
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   179
913
f1a5508f155e more verbose commenting of the sample
tomh@ns-regression.ee.washington.edu
parents: 901
diff changeset
   180
  std::cout << std::endl << "NonDefaultPrint()" << std::endl;
891
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   181
  NonDefaultPrint ();
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   182
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   183
  return 0;
83fb0d5f7038 sample code for pretty printing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   184
}