Skip to content

Commit 2a50852

Browse files
author
Felipe Zimmerle
committed
Using multiple threads in reading logs via rule message example
1 parent 8fbb9e8 commit 2a50852

File tree

3 files changed

+111
-62
lines changed

3 files changed

+111
-62
lines changed

Diff for: examples/reading_logs_via_rule_message/reading_logs_via_rule_message.h

+103-17
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,96 @@
1616
#include <string>
1717
#include <memory>
1818

19+
#include <unistd.h>
20+
21+
#define NUM_THREADS 100
22+
23+
24+
char request_header[] = "" \
25+
"GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1\n\r" \
26+
"Host: net.tutsplus.com\n\r" \
27+
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5)" \
28+
" Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)\n\r" \
29+
"Accept: text/html,application/xhtml+xml,application/xml; " \
30+
"q=0.9,*/*;q=0.8\n\r" \
31+
"Accept-Language: en-us,en;q=0.5\n\r" \
32+
"Accept-Encoding: gzip,deflate\n\r" \
33+
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n\r" \
34+
"Keep-Alive: 300\n\r" \
35+
"Connection: keep-alive\n\r" \
36+
"Cookie: PHPSESSID=r2t5uvjq435r4q7ib3vtdjq120\n\r" \
37+
"Pragma: no-cache\n\r" \
38+
"Cache-Control: no-cache\n\r";
39+
40+
char request_uri[] = "/test.pl?param1=test&para2=test2";
41+
42+
char request_body[] = "";
43+
44+
char response_headers[] = "" \
45+
"HTTP/1.1 200 OK\n\r" \
46+
"Content-Type: text/xml; charset=utf-8\n\r" \
47+
"Content-Length: length\n\r";
48+
49+
char response_body[] = "" \
50+
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\r" \
51+
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " \
52+
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " \
53+
"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n\r" \
54+
" <soap:Body>\n\r" \
55+
" <EnlightenResponse xmlns=\"http://clearforest.com/\">\n\r" \
56+
" <EnlightenResult>string</EnlightenResult>\n\r" \
57+
" </EnlightenResponse>\n\r" \
58+
" </soap:Body>\n\r" \
59+
"</soap:Envelope>\n\r";
60+
61+
char ip[] = "200.249.12.31";
62+
1963
#include "modsecurity/rule_message.h"
2064

2165
#ifndef EXAMPLES_READING_LOGS_VIA_RULE_MESSAGE_READING_LOGS_VIA_RULE_MESSAGE_H_
2266
#define EXAMPLES_READING_LOGS_VIA_RULE_MESSAGE_READING_LOGS_VIA_RULE_MESSAGE_H_
2367

2468

69+
struct data_ms {
70+
modsecurity::ModSecurity *modsec;
71+
modsecurity::Rules *rules;
72+
};
73+
74+
75+
static void *process_request(void *data) {
76+
struct data_ms *a = (struct data_ms *)data;
77+
modsecurity::ModSecurity *modsec = a->modsec;
78+
modsecurity::Rules *rules = a->rules;
79+
int z = 0;
80+
81+
for (z = 0; z < 10000; z++) {
82+
modsecurity::Transaction *modsecTransaction = \
83+
new modsecurity::Transaction(modsec, rules, NULL);
84+
modsecTransaction->processConnection(ip, 12345, "127.0.0.1", 80);
85+
modsecTransaction->processURI(request_uri, "GET", "1.1");
86+
87+
usleep(10);
88+
modsecTransaction->addRequestHeader("Host",
89+
"net.tutsplus.com");
90+
modsecTransaction->processRequestHeaders();
91+
modsecTransaction->processRequestBody();
92+
modsecTransaction->addResponseHeader("HTTP/1.1",
93+
"200 OK");
94+
modsecTransaction->processResponseHeaders(200, "HTTP 1.2");
95+
modsecTransaction->appendResponseBody(
96+
(const unsigned char*)response_body,
97+
strlen((const char*)response_body));
98+
modsecTransaction->processResponseBody();
99+
modsecTransaction->processLogging();
100+
101+
delete modsecTransaction;
102+
}
103+
104+
pthread_exit(NULL);
105+
return NULL;
106+
}
107+
108+
25109
class ReadingLogsViaRuleMessage {
26110
public:
27111
ReadingLogsViaRuleMessage(char *request_header,
@@ -41,6 +125,11 @@ class ReadingLogsViaRuleMessage {
41125
{ }
42126

43127
int process() {
128+
pthread_t threads[NUM_THREADS];
129+
int i;
130+
struct data_ms dms;
131+
void *status;
132+
44133
modsecurity::ModSecurity *modsec;
45134
modsecurity::Rules *rules;
46135
modsecurity::ModSecurityIntervention it;
@@ -58,27 +147,24 @@ class ReadingLogsViaRuleMessage {
58147
return -1;
59148
}
60149

61-
modsecurity::Transaction *modsecTransaction = \
62-
new modsecurity::Transaction(modsec, rules, NULL);
63-
modsecTransaction->processConnection(m_ip, 12345, "127.0.0.1", 80);
64-
modsecTransaction->processURI(m_request_uri, "GET", "1.1");
150+
dms.modsec = modsec;
151+
dms.rules = rules;
65152

66-
modsecTransaction->addRequestHeader("Host",
67-
"net.tutsplus.com");
68-
modsecTransaction->processRequestHeaders();
69-
modsecTransaction->processRequestBody();
70-
modsecTransaction->addResponseHeader("HTTP/1.1",
71-
"200 OK");
72-
modsecTransaction->processResponseHeaders(200, "HTTP 1.2");
73-
modsecTransaction->appendResponseBody(
74-
(const unsigned char*)m_response_body,
75-
strlen((const char*)m_response_body));
76-
modsecTransaction->processResponseBody();
77-
modsecTransaction->processLogging();
153+
for (i = 0; i < NUM_THREADS; i++) {
154+
pthread_create(&threads[i], NULL, process_request, (void *)&dms);
155+
//process_request((void *)&dms);
156+
}
157+
158+
usleep(10000);
159+
160+
for (i=0; i < NUM_THREADS; i++) {
161+
pthread_join(threads[i], &status);
162+
std::cout << "Main: completed thread id :" << i << std::endl;
163+
}
78164

79-
delete modsecTransaction;
80165
delete rules;
81166
delete modsec;
167+
pthread_exit(NULL);
82168
return 0;
83169
end:
84170
return -1;

Diff for: examples/reading_logs_via_rule_message/simple_request.cc

+7-41
Original file line numberDiff line numberDiff line change
@@ -18,60 +18,26 @@
1818

1919
#include <modsecurity/modsecurity.h>
2020
#include <modsecurity/rules.h>
21-
2221
#include "examples/reading_logs_via_rule_message/reading_logs_via_rule_message.h"
2322

24-
char request_header[] = "" \
25-
"GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1\n\r" \
26-
"Host: net.tutsplus.com\n\r" \
27-
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5)" \
28-
" Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)\n\r" \
29-
"Accept: text/html,application/xhtml+xml,application/xml; " \
30-
"q=0.9,*/*;q=0.8\n\r" \
31-
"Accept-Language: en-us,en;q=0.5\n\r" \
32-
"Accept-Encoding: gzip,deflate\n\r" \
33-
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n\r" \
34-
"Keep-Alive: 300\n\r" \
35-
"Connection: keep-alive\n\r" \
36-
"Cookie: PHPSESSID=r2t5uvjq435r4q7ib3vtdjq120\n\r" \
37-
"Pragma: no-cache\n\r" \
38-
"Cache-Control: no-cache\n\r";
39-
40-
char request_uri[] = "/test.pl?param1=test&para2=test2";
41-
42-
char request_body[] = "";
43-
44-
char response_headers[] = "" \
45-
"HTTP/1.1 200 OK\n\r" \
46-
"Content-Type: text/xml; charset=utf-8\n\r" \
47-
"Content-Length: length\n\r";
48-
49-
char response_body[] = "" \
50-
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\r" \
51-
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " \
52-
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " \
53-
"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n\r" \
54-
" <soap:Body>\n\r" \
55-
" <EnlightenResponse xmlns=\"http://clearforest.com/\">\n\r" \
56-
" <EnlightenResult>string</EnlightenResult>\n\r" \
57-
" </EnlightenResponse>\n\r" \
58-
" </soap:Body>\n\r" \
59-
"</soap:Envelope>\n\r";
60-
61-
char ip[] = "200.249.12.31";
6223

6324

6425
int main(int argc, char **argv) {
65-
(*argv)++;
26+
*argv++;
6627
if (*argv == NULL) {
67-
(*argv)--;
28+
*argv--;
6829
std::cout << "Use " << *argv << " test-case-file.conf";
6930
std::cout << std::endl << std::endl;
7031
return -1;
7132
}
33+
7234
std::string rules(*argv);
7335
ReadingLogsViaRuleMessage rlvrm(request_header, request_uri, request_body,
7436
response_headers, response_body, ip, rules);
7537
rlvrm.process();
38+
39+
40+
41+
pthread_exit(NULL);
7642
return 0;
7743
}

Diff for: src/collection/backend/in_memory-per_process.cc

+1-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ namespace backend {
3838

3939
InMemoryPerProcess::InMemoryPerProcess() {
4040
this->reserve(1000);
41-
if (pthread_mutex_init(&m_lock, NULL) != 0)
42-
{
43-
printf("\n mutex init failed\n");
44-
}
41+
pthread_mutex_init(&m_lock, NULL);
4542
}
4643

4744
InMemoryPerProcess::~InMemoryPerProcess() {

0 commit comments

Comments
 (0)