|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
|
|
2 |
/* |
3 |
* Copyright (c) 2006,2007 INRIA |
4 |
* All rights reserved. |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License version 2 as |
8 |
* published by the Free Software Foundation; |
9 |
* |
10 |
* This program is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
* GNU General Public License for more details. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License |
16 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 |
* |
19 |
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
20 |
*/ |
21 |
|
2 |
#include "ns3/packet.h" |
22 |
#include "ns3/packet.h" |
3 |
#include "ns3/header.h" |
23 |
#include "ns3/header.h" |
4 |
#include "ns3/packet-printer.h" |
24 |
#include "ns3/packet-printer.h" |
|
7 |
|
27 |
|
8 |
using namespace ns3; |
28 |
using namespace ns3; |
9 |
|
29 |
|
|
|
30 |
// This sample file shows how to use the Packet metadata facility |
31 |
// |
32 |
// Packets are stored as ``packed'' data structures, to facilitate |
33 |
// fragmentation and network emulation. However, when debugging a program, |
34 |
// or for certain tracing applications, it may be convenient to dump out |
35 |
// the contents of a packet header in a human-friendly form. |
36 |
// |
37 |
// To do this, a few things are needed: |
38 |
// i) enable the metadata facility (disabled by default, because it causes |
39 |
// a small performance hit |
40 |
// ii) decide on whether you want to use a default or customized (you |
41 |
// provide your own) routine to dump a particular header |
42 |
// |
43 |
// This sample steps through two routines; one to use the default |
44 |
// printing of IPv4 and UDP headers, and one to show a non-default case. |
45 |
// There is a lot of emphasis in this sample of how this facility |
46 |
// interacts with packet fragmentation. |
10 |
|
47 |
|
11 |
void DefaultPrint (void) |
48 |
void DefaultPrint (void) |
12 |
{ |
49 |
{ |
Lines 19-28
void DefaultPrint (void)
|
Link Here
|
---|
|
19 |
ipv4.SetDestination (Ipv4Address ("192.168.0.2")); |
56 |
ipv4.SetDestination (Ipv4Address ("192.168.0.2")); |
20 |
udp.SetSource (1025); |
57 |
udp.SetSource (1025); |
21 |
udp.SetDestination (80); |
58 |
udp.SetDestination (80); |
|
|
59 |
udp.SetPayloadSize (1000); |
22 |
p.AddHeader (udp); |
60 |
p.AddHeader (udp); |
23 |
p.AddHeader (ipv4); |
61 |
p.AddHeader (ipv4); |
24 |
|
62 |
|
25 |
std::cout << "full packet size=" << p.GetSize () << std::endl; |
63 |
std::cout << "full packet size=" << p.GetSize () << std::endl; |
|
|
64 |
// Here, invoke the default Print routine, directed to std out |
26 |
p.Print (std::cout); |
65 |
p.Print (std::cout); |
27 |
std::cout << std::endl; |
66 |
std::cout << std::endl; |
28 |
|
67 |
|
Lines 51-56
void DefaultPrint (void)
|
Link Here
|
---|
|
51 |
std::cout << std::endl; |
90 |
std::cout << std::endl; |
52 |
} |
91 |
} |
53 |
|
92 |
|
|
|
93 |
// The below functions are used in place of default versions, in the |
94 |
// non-default case below. For instance, DoPrintIpv4Header will print |
95 |
// out less IPv4 header information than the default print function |
54 |
void |
96 |
void |
55 |
DoPrintDefault (std::ostream &os,uint32_t packetUid, uint32_t size, |
97 |
DoPrintDefault (std::ostream &os,uint32_t packetUid, uint32_t size, |
56 |
std::string &name, struct PacketPrinter::FragmentInformation info) |
98 |
std::string &name, struct PacketPrinter::FragmentInformation info) |
Lines 75-80
DoPrintIpv4HeaderFragment (std::ostream
|
Link Here
|
---|
|
75 |
os << "IPV4 fragment"; |
117 |
os << "IPV4 fragment"; |
76 |
} |
118 |
} |
77 |
|
119 |
|
|
|
120 |
// This function walks through a non-default case. A few features of |
121 |
// the API (defined in common/packet-printer.h) are shown. |
122 |
// |
78 |
void NonDefaultPrint (void) |
123 |
void NonDefaultPrint (void) |
79 |
{ |
124 |
{ |
80 |
// create an adhoc packet printer. |
125 |
// create an adhoc packet printer. |
Lines 103-108
void NonDefaultPrint (void)
|
Link Here
|
---|
|
103 |
ipv4.SetDestination (Ipv4Address ("192.168.0.2")); |
148 |
ipv4.SetDestination (Ipv4Address ("192.168.0.2")); |
104 |
udp.SetSource (1025); |
149 |
udp.SetSource (1025); |
105 |
udp.SetDestination (80); |
150 |
udp.SetDestination (80); |
|
|
151 |
udp.SetPayloadSize (1000); |
106 |
p.AddHeader (udp); |
152 |
p.AddHeader (udp); |
107 |
p.AddHeader (ipv4); |
153 |
p.AddHeader (ipv4); |
108 |
|
154 |
|
Lines 141-148
int main (int argc, char *argv[])
|
Link Here
|
---|
|
141 |
{ |
187 |
{ |
142 |
Packet::EnableMetadata (); |
188 |
Packet::EnableMetadata (); |
143 |
|
189 |
|
|
|
190 |
std::cout << "DefaultPrint()" << std::endl; |
144 |
DefaultPrint (); |
191 |
DefaultPrint (); |
145 |
|
192 |
|
|
|
193 |
std::cout << std::endl << "NonDefaultPrint()" << std::endl; |
146 |
NonDefaultPrint (); |
194 |
NonDefaultPrint (); |
147 |
|
195 |
|
148 |
return 0; |
196 |
return 0; |