-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndn-consumer-ddos.cpp
170 lines (140 loc) · 4.89 KB
/
ndn-consumer-ddos.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2011-2015 Regents of the University of California.
*
* This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
* contributors.
*
* ndnSIM is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
**/
#include "ndn-consumer-ddos.hpp"
#include "ns3/ptr.h"
#include "ns3/log.h"
#include "ns3/simulator.h"
#include "ns3/packet.h"
#include "ns3/callback.h"
#include "ns3/string.h"
#include "ns3/boolean.h"
#include "ns3/uinteger.h"
#include "ns3/integer.h"
#include "ns3/double.h"
NS_LOG_COMPONENT_DEFINE("ndn.Consumerddos");
namespace ns3 {
namespace ndn {
NS_OBJECT_ENSURE_REGISTERED(Consumerddos);
void
Consumerddos::SendPacket()
{
if (!m_active)
return;
NS_LOG_FUNCTION_NOARGS();
uint32_t seq = std::numeric_limits<uint32_t>::max(); // invalid
while (m_retxSeqs.size()) {
seq = *m_retxSeqs.begin();
m_retxSeqs.erase(m_retxSeqs.begin());
break;
}
if (seq == std::numeric_limits<uint32_t>::max()) {
if (m_seqMax != std::numeric_limits<uint32_t>::max()) {
if (m_seq >= m_seqMax) {
return; // we are totally done
}
}
seq = m_seq++;
}
//
shared_ptr<Name> nameWithSequence = make_shared<Name>(m_interestName);
nameWithSequence->appendSequenceNumber(seq);
//
// shared_ptr<Interest> interest = make_shared<Interest> ();
shared_ptr<Interest> interest = make_shared<Interest>();
interest->setNonce(m_rand->GetValue(0, std::numeric_limits<uint32_t>::max()));
interest->setName(*nameWithSequence);
interest->setCanBePrefix(false);
time::milliseconds interestLifeTime(m_interestLifeTime.GetMilliSeconds());
interest->setInterestLifetime(interestLifeTime);
// NS_LOG_INFO ("Requesting Interest: \n" << *interest);
NS_LOG_INFO("> Interest for " << seq);
WillSendOutInterest(seq);
m_transmittedInterests(interest, this, m_face);
m_appLink->onReceiveInterest(*interest);
ScheduleNextPacket();
}
TypeId
Consumerddos::GetTypeId(void)
{
static TypeId tid =
TypeId("ns3::ndn::Consumerddos")
.SetGroupName("Ndn")
.SetParent<Consumer>()
.AddConstructor<Consumerddos>()
.AddAttribute("Frequency", "Frequency of interest packets", StringValue("1.0"),
MakeDoubleAccessor(&Consumerddos::m_frequency), MakeDoubleChecker<double>())
.AddAttribute("Randomize",
"Type of send time randomization: none (default), uniform, exponential",
StringValue("none"),
MakeStringAccessor(&Consumerddos::SetRandomize, &Consumerddos::GetRandomize),
MakeStringChecker())
.AddAttribute("MaxSeq", "Maximum sequence number to request",
IntegerValue(std::numeric_limits<uint32_t>::max()),
MakeIntegerAccessor(&Consumerddos::m_seqMax), MakeIntegerChecker<uint32_t>())
;
return tid;
}
Consumerddos::Consumerddos()
: m_frequency(1.0)
, m_firstTime(true)
{
NS_LOG_FUNCTION_NOARGS();
m_seqMax = std::numeric_limits<uint32_t>::max();
}
Consumerddos::~Consumerddos()
{
}
void
Consumerddos::ScheduleNextPacket()
{
// double mean = 8.0 * m_payloadSize / m_desiredRate.GetBitRate ();
// std::cout << "next: " << Simulator::Now().ToDouble(Time::S) + mean << "s\n";
if (m_firstTime) {
m_sendEvent = Simulator::Schedule(Seconds(0.0), &Consumer::SendPacket, this);
m_firstTime = false;
}
else if (!m_sendEvent.IsRunning())
m_sendEvent = Simulator::Schedule((m_random == 0) ? Seconds(1.0 / m_frequency)
: Seconds(m_random->GetValue()),
&Consumer::SendPacket, this);
}
void
Consumerddos::SetRandomize(const std::string& value)
{
if (value == "uniform") {
m_random = CreateObject<UniformRandomVariable>();
m_random->SetAttribute("Min", DoubleValue(0.0));
m_random->SetAttribute("Max", DoubleValue(2 * 1.0 / m_frequency));
}
else if (value == "exponential") {
m_random = CreateObject<ExponentialRandomVariable>();
m_random->SetAttribute("Mean", DoubleValue(1.0 / m_frequency));
m_random->SetAttribute("Bound", DoubleValue(50 * 1.0 / m_frequency));
}
else
m_random = 0;
m_randomType = value;
}
std::string
Consumerddos::GetRandomize() const
{
return m_randomType;
}
} // namespace ndn
} // namespace ns3