Skip to content

Commit b9b22a7

Browse files
authored
Merge pull request #341 from FedeParola/ddosmitigator-fixes
ddosmitigator: handle insertion of already present address
2 parents 8bd4c5c + 731aec0 commit b9b22a7

File tree

1 file changed

+45
-41
lines changed

1 file changed

+45
-41
lines changed

src/services/pcn-ddosmitigator/src/Ddosmitigator.cpp

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -176,30 +176,32 @@ Ddosmitigator::getBlacklistSrcList() {
176176

177177
void Ddosmitigator::addBlacklistSrc(const std::string &ip,
178178
const BlacklistSrcJsonObject &conf) {
179-
logger()->debug("BlacklistSrc create");
180-
181-
try {
182-
logger()->debug("blacklist size {0} ", blacklistsrc_.size());
183-
// TODO check if src ip rules are already present
184-
// and reinject datapath with srcblacklist ps
185-
186-
if (blacklistsrc_.size() >= 0) {
187-
setSrcMatch(true);
188-
reloadCode();
179+
if (blacklistsrc_.count(ip) == 0) {
180+
logger()->debug("BlacklistSrc create");
181+
182+
try {
183+
logger()->debug("blacklist size {0} ", blacklistsrc_.size());
184+
// TODO check if src ip rules are already present
185+
// and reinject datapath with srcblacklist ps
186+
187+
if (blacklistsrc_.size() >= 0) {
188+
setSrcMatch(true);
189+
reloadCode();
190+
}
191+
192+
auto srcblacklist =
193+
get_percpuhash_table<uint32_t, uint64_t>("srcblacklist");
194+
srcblacklist.set(utils::ip_string_to_nbo_uint(ip), 0);
195+
} catch (...) {
196+
throw std::runtime_error("unable to add element to map");
189197
}
190198

191-
auto srcblacklist =
192-
get_percpuhash_table<uint32_t, uint64_t>("srcblacklist");
193-
srcblacklist.set(utils::ip_string_to_nbo_uint(ip), 0);
194-
} catch (...) {
195-
throw std::runtime_error("unable to add element to map");
196-
}
199+
BlacklistSrcJsonObject configuration;
200+
configuration.setIp(ip);
197201

198-
BlacklistSrcJsonObject configuration;
199-
configuration.setIp(ip);
200-
201-
blacklistsrc_.emplace(std::piecewise_construct, std::forward_as_tuple(ip),
202-
std::forward_as_tuple(*this, configuration));
202+
blacklistsrc_.emplace(std::piecewise_construct, std::forward_as_tuple(ip),
203+
std::forward_as_tuple(*this, configuration));
204+
}
203205
}
204206

205207
void Ddosmitigator::addBlacklistSrcList(
@@ -264,29 +266,31 @@ Ddosmitigator::getBlacklistDstList() {
264266

265267
void Ddosmitigator::addBlacklistDst(const std::string &ip,
266268
const BlacklistDstJsonObject &conf) {
267-
logger()->debug("BlacklistDst create");
268-
269-
try {
270-
// TODO check if dst ip rules are already present
271-
// and reinject datapath with dstblacklist ps
272-
273-
if (blacklistdst_.size() >= 0) {
274-
setDstMatch(true);
275-
reloadCode();
269+
if (blacklistdst_.count(ip) == 0) {
270+
logger()->debug("BlacklistDst create");
271+
272+
try {
273+
// TODO check if dst ip rules are already present
274+
// and reinject datapath with dstblacklist ps
275+
276+
if (blacklistdst_.size() >= 0) {
277+
setDstMatch(true);
278+
reloadCode();
279+
}
280+
281+
auto dstblacklist =
282+
get_percpuhash_table<uint32_t, uint64_t>("dstblacklist");
283+
dstblacklist.set(utils::ip_string_to_nbo_uint(ip), 0);
284+
} catch (...) {
285+
throw std::runtime_error("unable to add element to map");
276286
}
277287

278-
auto dstblacklist =
279-
get_percpuhash_table<uint32_t, uint64_t>("dstblacklist");
280-
dstblacklist.set(utils::ip_string_to_nbo_uint(ip), 0);
281-
} catch (...) {
282-
throw std::runtime_error("unable to add element to map");
283-
}
288+
BlacklistDstJsonObject configuration;
289+
configuration.setIp(ip);
284290

285-
BlacklistDstJsonObject configuration;
286-
configuration.setIp(ip);
287-
288-
blacklistdst_.emplace(std::piecewise_construct, std::forward_as_tuple(ip),
289-
std::forward_as_tuple(*this, configuration));
291+
blacklistdst_.emplace(std::piecewise_construct, std::forward_as_tuple(ip),
292+
std::forward_as_tuple(*this, configuration));
293+
}
290294
}
291295

292296
void Ddosmitigator::addBlacklistDstList(

0 commit comments

Comments
 (0)