1.1 --- a/samples/main-packet-printer.cc Thu Oct 01 22:49:25 2009 -0700
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,183 +0,0 @@
1.4 -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
1.5 -/*
1.6 - * Copyright (c) 2006,2007 INRIA
1.7 - *
1.8 - * This program is free software; you can redistribute it and/or modify
1.9 - * it under the terms of the GNU General Public License version 2 as
1.10 - * published by the Free Software Foundation;
1.11 - *
1.12 - * This program is distributed in the hope that it will be useful,
1.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 - * GNU General Public License for more details.
1.16 - *
1.17 - * You should have received a copy of the GNU General Public License
1.18 - * along with this program; if not, write to the Free Software
1.19 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.20 - *
1.21 - * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
1.22 - */
1.23 -
1.24 -#include "ns3/packet.h"
1.25 -#include "ns3/header.h"
1.26 -#include "ns3/packet-printer.h"
1.27 -#include "ns3/ipv4-header.h"
1.28 -#include "ns3/udp-header.h"
1.29 -
1.30 -using namespace ns3;
1.31 -
1.32 -// This sample file shows how to use the Packet metadata facility
1.33 -//
1.34 -// Packets are stored as ``packed'' data structures, to facilitate
1.35 -// fragmentation and network emulation. However, when debugging a program,
1.36 -// or for certain tracing applications, it may be convenient to dump out
1.37 -// the contents of a packet header in a human-friendly form.
1.38 -//
1.39 -// To do this, a few things are needed:
1.40 -// i) enable the metadata facility (disabled by default, because it causes
1.41 -// a small performance hit
1.42 -// ii) decide on whether you want to use a default or customized (you
1.43 -// provide your own) routine to dump a particular header
1.44 -//
1.45 -// This sample steps through two routines; one to use the default
1.46 -// printing of IPv4 and UDP headers, and one to show a non-default case.
1.47 -// There is a lot of emphasis in this sample of how this facility
1.48 -// interacts with packet fragmentation.
1.49 -
1.50 -void DefaultPrint (void)
1.51 -{
1.52 - // We create a packet with 1000 bytes of zero payload
1.53 - // and add 3 headers to this packet.
1.54 - Ptr<Packet> p = Create<Packet> (1000);
1.55 - Ipv4Header ipv4;
1.56 - UdpHeader udp;
1.57 - ipv4.SetSource (Ipv4Address ("192.168.0.1"));
1.58 - ipv4.SetDestination (Ipv4Address ("192.168.0.2"));
1.59 - udp.SetSource (1025);
1.60 - udp.SetDestination (80);
1.61 - udp.SetPayloadSize (1000);
1.62 - p->AddHeader (udp);
1.63 - p->AddHeader (ipv4);
1.64 -
1.65 - std::cout << "full packet size=" << p->GetSize () << std::endl;
1.66 - // Here, invoke the default Print routine, directed to std out
1.67 - p->Print (std::cout);
1.68 - std::cout << std::endl;
1.69 -
1.70 -
1.71 - // Now, we fragment our packet in 3 consecutive pieces.
1.72 - Ptr<Packet> p1 = p->CreateFragment (0, 2);
1.73 - Ptr<Packet> p2 = p->CreateFragment (2, 1000);
1.74 - Ptr<Packet> p3 = p->CreateFragment (1002, 26);
1.75 -
1.76 - std::cout << "fragment1" << std::endl;
1.77 - p1->Print (std::cout);
1.78 - std::cout << std::endl;
1.79 - std::cout << "fragment2" << std::endl;
1.80 - p2->Print (std::cout);
1.81 - std::cout << std::endl;
1.82 - std::cout << "fragment3" << std::endl;
1.83 - p3->Print (std::cout);
1.84 - std::cout << std::endl;
1.85 -
1.86 - // And, finally, we re-aggregate the 3 consecutive pieces.
1.87 - Ptr<Packet> aggregate = p1->Copy ();
1.88 - aggregate->AddAtEnd (p2);
1.89 - aggregate->AddAtEnd (p3);
1.90 - std::cout << "aggregated" << std::endl;
1.91 - aggregate->Print (std::cout);
1.92 - std::cout << std::endl;
1.93 -}
1.94 -
1.95 -void
1.96 -DoPrintPayload (std::ostream & os,uint32_t packetUid,uint32_t size,
1.97 - struct PacketPrinter::FragmentInformation info)
1.98 -{
1.99 - os << "PAYLOAD (size " << size << " trim_start " << info.start << " trim_end " << info.end << ")";
1.100 -}
1.101 -void
1.102 -DoPrintIpv4Header (std::ostream &os, uint32_t packetUid, uint32_t size, const Ipv4Header *ipv4)
1.103 -{
1.104 - os << "IPV4 " << ipv4->GetSource () << " > " << ipv4->GetDestination ();
1.105 -}
1.106 -void
1.107 -DoPrintIpv4HeaderFragment (std::ostream &os, uint32_t packetUid, uint32_t size,
1.108 - std::string &name, struct PacketPrinter::FragmentInformation info)
1.109 -{
1.110 - os << "IPV4 fragment";
1.111 -}
1.112 -
1.113 -// This function walks through a non-default case. A few features of
1.114 -// the API (defined in common/packet-printer.h) are shown.
1.115 -//
1.116 -void NonDefaultPrint (void)
1.117 -{
1.118 - // create an adhoc packet printer.
1.119 - PacketPrinter printer;
1.120 - // print from first header to last trailer
1.121 - printer.PrintForward ();
1.122 - // set a string separator automatically inserted
1.123 - // between each call to a printing function.
1.124 - printer.SetSeparator (" - ");
1.125 - // set the payload print function
1.126 - printer.SetPayloadPrinter (MakeCallback (&DoPrintPayload));
1.127 - // set the print function for the header type Ipv4Header.
1.128 - printer.SetHeaderPrinter (MakeCallback (&DoPrintIpv4Header),
1.129 - MakeCallback (&DoPrintIpv4HeaderFragment));
1.130 -
1.131 -
1.132 - // We create a packet with 1000 bytes of zero payload
1.133 - Ptr<Packet> p = Create<Packet> (1000);
1.134 - Ipv4Header ipv4;
1.135 - UdpHeader udp;
1.136 - ipv4.SetSource (Ipv4Address ("192.168.0.1"));
1.137 - ipv4.SetDestination (Ipv4Address ("192.168.0.2"));
1.138 - udp.SetSource (1025);
1.139 - udp.SetDestination (80);
1.140 - udp.SetPayloadSize (1000);
1.141 - p->AddHeader (udp);
1.142 - p->AddHeader (ipv4);
1.143 -
1.144 - std::cout << "full packet size=" << p->GetSize () << std::endl;
1.145 - p->Print (std::cout, printer);
1.146 - std::cout << std::endl;
1.147 -
1.148 -
1.149 - // fragment our packet in 3 pieces
1.150 - Ptr<Packet> p1 = p->CreateFragment (0, 2);
1.151 - Ptr<Packet> p2 = p->CreateFragment (2, 1000);
1.152 - Ptr<Packet> p3 = p->CreateFragment (1002, 26);
1.153 - std::cout << "fragment1" << std::endl;
1.154 - p1->Print (std::cout, printer);
1.155 - std::cout << std::endl;
1.156 - std::cout << "fragment2" << std::endl;
1.157 - p2->Print (std::cout, printer);
1.158 - std::cout << std::endl;
1.159 - std::cout << "fragment3" << std::endl;
1.160 - p3->Print (std::cout, printer);
1.161 - std::cout << std::endl;
1.162 -
1.163 - // aggregate all 3 fragments of the original packet
1.164 - // to reconstruct a copy of the original packet.
1.165 - Ptr<Packet> aggregate = p1->Copy ();
1.166 - aggregate->AddAtEnd (p2);
1.167 - aggregate->AddAtEnd (p3);
1.168 - std::cout << "aggregated" << std::endl;
1.169 - aggregate->Print (std::cout, printer);
1.170 - std::cout << std::endl;
1.171 -}
1.172 -
1.173 -
1.174 -
1.175 -int main (int argc, char *argv[])
1.176 -{
1.177 - Packet::EnableMetadata ();
1.178 -
1.179 - std::cout << "DefaultPrint()" << std::endl;
1.180 - DefaultPrint ();
1.181 -
1.182 - std::cout << std::endl << "NonDefaultPrint()" << std::endl;
1.183 - NonDefaultPrint ();
1.184 -
1.185 - return 0;
1.186 -}