blob: 12a0cbbb34d128eb02ceec12c122c2251aa78fc9 [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -08004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -08007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -080011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -080015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20// ndn-simple-with-pcap.cpp
21
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070022#include "ns3/core-module.h"
23#include "ns3/network-module.h"
24#include "ns3/point-to-point-module.h"
25#include "ns3/ndnSIM-module.h"
26
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080027/**
28 * This scenario demonstrates how to dump raw NDN packets into tcpdump-format
29 *
30 * Run scenario:
31 *
32 * ./waf --run ndn-simple-with-pcap
33 *
34 * After simulation finishes, it produces `ndn-simple-trace.pcap` that can be read
35 * using tcpdump or ndndump tools:
36 *
37 * ndndump -r ndn-simple-trace.pcap not ip
38 * tcpdump -r ndn-simple-trace.pcap
39 */
40
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -080041namespace ns3 {
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070042
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080043class PcapWriter {
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070044public:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080045 PcapWriter(const std::string& file)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070046 {
47 PcapHelper helper;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080048 m_pcap = helper.CreateFile(file, std::ios::out, PcapHelper::DLT_PPP);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070049 }
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -080050
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070051 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080052 TracePacket(Ptr<const Packet> packet)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070053 {
54 static PppHeader pppHeader;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080055 pppHeader.SetProtocol(0x0077);
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -080056
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057 m_pcap->Write(Simulator::Now(), pppHeader, packet);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070058 }
59
60private:
61 Ptr<PcapFileWrapper> m_pcap;
62};
63
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070064int
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080065main(int argc, char* argv[])
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070066{
67 // setting default parameters for PointToPoint links and channels
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068 Config::SetDefault("ns3::PointToPointNetDevice::DataRate", StringValue("1Mbps"));
69 Config::SetDefault("ns3::PointToPointChannel::Delay", StringValue("10ms"));
70 Config::SetDefault("ns3::DropTailQueue::MaxPackets", StringValue("20"));
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070071
Alexander Afanasyev39314e02013-08-13 10:10:45 -070072 // Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize
73 CommandLine cmd;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074 cmd.Parse(argc, argv);
75
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070076 // Creating nodes
77 NodeContainer nodes;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080078 nodes.Create(3);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070079
80 // Connecting nodes using two links
81 PointToPointHelper p2p;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080082 p2p.Install(nodes.Get(0), nodes.Get(1));
83 p2p.Install(nodes.Get(1), nodes.Get(2));
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070084
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070085 // Install NDN stack on all nodes
86 ndn::StackHelper ndnHelper;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087 ndnHelper.SetDefaultRoutes(true);
88 ndnHelper.InstallAll();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070089
90 // Installing applications
91
92 // Consumer
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080093 ndn::AppHelper consumerHelper("ns3::ndn::ConsumerCbr");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070094 // Consumer will request /prefix/0, /prefix/1, ...
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080095 consumerHelper.SetPrefix("/prefix");
96 consumerHelper.SetAttribute("Frequency", StringValue("10")); // 10 interests a second
97 consumerHelper.Install(nodes.Get(0)); // first node
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070098
99 // Producer
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100 ndn::AppHelper producerHelper("ns3::ndn::Producer");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700101 // Producer will reply to all requests starting with /prefix
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102 producerHelper.SetPrefix("/prefix");
103 producerHelper.SetAttribute("PayloadSize", StringValue("1024"));
104 producerHelper.SetAttribute("Signature", UintegerValue(100));
105 producerHelper.SetAttribute("KeyLocator", StringValue("/unique/key/locator"));
106 producerHelper.Install(nodes.Get(2)); // last node
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700107
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800108 PcapWriter trace("ndn-simple-trace.pcap");
109 Config::ConnectWithoutContext("/NodeList/*/DeviceList/*/$ns3::PointToPointNetDevice/MacTx",
110 MakeCallback(&PcapWriter::TracePacket, &trace));
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -0800111
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800112 Simulator::Stop(Seconds(20.0));
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700113
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800114 Simulator::Run();
115 Simulator::Destroy();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700116
117 return 0;
118}
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -0800119
120} // namespace ns3
121
122int
123main(int argc, char* argv[])
124{
125 return ns3::main(argc, argv);
126}