Skip to content

Commit 436cf35

Browse files
committed
Adding multithreaded example from issue #3054 (by airween)
- Rewritten to use C++ libModSecurity API and std::thread (instead of pthreads)
1 parent 3d9027b commit 436cf35

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

build/win32/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ setExampleTargetProperties(reading_logs_via_rule_message)
259259
add_executable(reading_logs_with_offset ${BASE_DIR}/examples/reading_logs_with_offset/read.cc)
260260
setExampleTargetProperties(reading_logs_with_offset)
261261

262+
# multithread_c
263+
add_executable(multithread_c ${BASE_DIR}/examples/multithread_c/multithr.cc)
264+
setExampleTargetProperties(multithread_c)
265+
262266
# tools
263267
#
264268

examples/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ ACLOCAL_AMFLAGS = -I build
44

55
SUBDIRS = \
66
multiprocess_c \
7+
multithread_c \
78
reading_logs_with_offset \
89
reading_logs_via_rule_message \
910
simple_example_using_c \

examples/multithread_c/Makefile.am

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
3+
noinst_PROGRAMS = multithr
4+
5+
multithr_SOURCES = \
6+
multithr.cc
7+
8+
multi_LDADD = \
9+
$(SSDEEP_LDADD) \
10+
$(LUA_LDADD) \
11+
$(MAXMIND_LDADD) \
12+
$(GLOBAL_LDADD)
13+
14+
multi_LDFLAGS = \
15+
-L$(top_builddir)/src/.libs/ \
16+
$(GEOIP_LDFLAGS) \
17+
-lmodsecurity \
18+
-lm \
19+
-lstdc++ \
20+
$(LUA_LDFLAGS) \
21+
$(SSDEEP_LDFLAGS) \
22+
$(MAXMIND_LDFLAGS) \
23+
$(YAJL_LDFLAGS)
24+
25+
multi_CFLAGS = \
26+
-I$(top_builddir)/headers \
27+
-I$(top_builddir) \
28+
$(GLOBAL_CFLAGS)
29+
30+
MAINTAINERCLEANFILES = \
31+
Makefile.in
32+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
SecDebugLog debug.log
2+
SecDebugLogLevel 9
3+
4+
5+
SecRule REQUEST_HEADERS:User-Agent ".*" "id:1,phase:1,t:sha1,t:hexEncode,setvar:tx.ua_hash=%{MATCHED_VAR}"
6+
7+
SecAction "id:2,phase:2,initcol:ip=%{REMOTE_ADDR}_%{tx.ua_hash}"
8+
9+
SecRule REQUEST_HEADERS:User-Agent "@rx .*" "id:3,phase:2,setvar:ip.auth_attempt=+1"
10+
11+
SecRule ARGS:foo "@rx herewego" "id:4,phase:2,setvar:ip.foo=bar,expirevar:ip.foo=2"
12+
#SecRule ARGS:foo "@rx herewego" "id:4,phase:2,setvar:ip.foo=bar"
13+
SecRule IP "@rx bar" "id:5,phase:2,pass"
14+
SecRule IP:auth_attempt "@rx bar" "id:6,phase:2,pass"

examples/multithread_c/multithr.cc

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <iostream>
2+
#include <thread>
3+
#include <array>
4+
5+
#include <modsecurity/modsecurity.h>
6+
#include <modsecurity/transaction.h>
7+
#include <modsecurity/rules_set.h>
8+
9+
static void process_request(modsecurity::ModSecurity *modsec, modsecurity::RulesSet *rules, int tid) {
10+
std::cout << "Hello World! It's me, thread #" << tid << std::endl;
11+
12+
for(int i = 0; i != 10'000; i++) {
13+
auto modsecTransaction = std::make_unique<modsecurity::Transaction>(modsec, rules, nullptr);
14+
15+
modsecTransaction->processConnection("127.0.0.1", 12345, "127.0.0.1", 80);
16+
modsecTransaction->processURI(
17+
"http://www.modsecurity.org/test?foo=herewego",
18+
"GET", "1.1");
19+
20+
modsecTransaction->addRequestHeader("User-Agent",
21+
"Basic ModSecurity example");
22+
modsecTransaction->processRequestHeaders();
23+
modsecTransaction->processRequestBody();
24+
25+
modsecTransaction->addResponseHeader("HTTP/1.1",
26+
"200 OK");
27+
modsecTransaction->processResponseHeaders(200, "HTTP 1.2");
28+
modsecTransaction->processResponseBody();
29+
30+
modsecTransaction->processLogging();
31+
32+
std::this_thread::sleep_for(std::chrono::microseconds(100));
33+
}
34+
35+
std::cout << "Thread #" << tid << " exits" << std::endl;
36+
}
37+
38+
int main (int argc, char *argv[]) {
39+
auto modsec = std::make_unique<modsecurity::ModSecurity>();
40+
modsec->setConnectorInformation("ModSecurity-test v0.0.1-alpha (Simple " \
41+
"example on how to use ModSecurity API");
42+
43+
char main_rule_uri[] = "basic_rules.conf";
44+
auto rules = std::make_unique<modsecurity::RulesSet>();
45+
if (rules->loadFromUri(main_rule_uri) < 0) {
46+
std::cerr << "Problems loading the rules..." << std::endl;
47+
std::cerr << rules->m_parserError.str() << std::endl;
48+
return -1;
49+
}
50+
51+
constexpr auto NUM_THREADS = 100;
52+
std::array<std::thread, NUM_THREADS> threads;
53+
54+
for (auto i = 0; i != threads.size(); ++i) {
55+
threads[i] = std::thread(
56+
[&modsec, &rules, i]() {
57+
process_request(modsec.get(), rules.get(), i);
58+
});
59+
}
60+
61+
std::this_thread::sleep_for(std::chrono::microseconds(10000));
62+
63+
for (auto i = 0; i != threads.size(); ++i) {
64+
threads[i].join();
65+
}
66+
67+
return 0;
68+
}

0 commit comments

Comments
 (0)