-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIsBot.cpp
76 lines (57 loc) · 1.83 KB
/
IsBot.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
/**
* \file IsBot.cpp
* \brief
*
* \review
*
* http://xpoint.ru/forums/programming/perl/misc/thread/32675.xhtml
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
//--------------------------------------------------------------------------------------------------
template<class T, size_t N>
constexpr size_t arraySize(T (&)[N]) { return N; }
//--------------------------------------------------------------------------------------------------
constexpr size_t blackList[][2]
{
#include "BlackList.lst"
};
const size_t blackListSize = arraySize(blackList);
//--------------------------------------------------------------------------------------------------
bool
isBot(const char *a_ip)
{
STD_TEST_PTR(a_ip);
std::cout << STD_TRACE_VAR(blackListSize) << std::endl << std::endl;
const auto ip = ntohl( inet_addr(a_ip) );
STD_TEST(ip > 0);
for (size_t i = 0 ; i < blackListSize; ++ i) {
const auto _ip_test = blackList[i][0];
const auto _mask_test = blackList[i][1];
const bool bRv = (_ip_test == (ip & _mask_test));
if (!bRv) {
std::cout << STD_TRACE_VAR3(_ip_test, _mask_test, bRv) << std::endl << std::endl;
return true;
}
}
return false;
}
//--------------------------------------------------------------------------------------------------
int main(int, char **)
{
const char ip[] = "209.185.108.10";
const bool bRv = ::isBot(ip);
STD_TEST(bRv);
std::cout << STD_TRACE_VAR2(ip, bRv) << std::endl;
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
#if 0
blackListSize: 4
_ip_test: 3518627072, _mask_test: 4294967040, bRv: 0
ip: 209.185.108.10, bRv: 1
#endif