Skip to content

Commit bb200f8

Browse files
committed
Implement and use Buffer class
1 parent e5d7c10 commit bb200f8

8 files changed

Lines changed: 221 additions & 92 deletions

File tree

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ pcap_http_analyzer_SOURCES = \
44
main.cc \
55
args.cc \
66
args.h \
7+
buffer.cc \
8+
buffer.h \
79
commparty.cc \
810
commparty.h \
911
print.cc \

src/buffer.cc

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#ifdef HAVE_CONFIG_H
2+
#include "config.h"
3+
#endif /* HAVE_CONFIG_H */
4+
5+
#include "buffer.h"
6+
7+
#include <stdlib.h>
8+
#include <string.h>
9+
10+
Buffer::Buffer(const char* data, unsigned int length) {
11+
mData = (char*) malloc(length);
12+
mLength = length;
13+
memcpy(mData, data, length);
14+
}
15+
16+
Buffer::Buffer(const Buffer& other) : Buffer(other.mData, other.mLength) {
17+
}
18+
19+
Buffer::~Buffer() {
20+
if (mData) {
21+
free(mData);
22+
}
23+
}
24+
25+
Buffer&
26+
Buffer:: operator=(const Buffer& rhs) {
27+
if (mData) {
28+
free(mData);
29+
}
30+
31+
mLength = rhs.mLength;
32+
mData = (char*) malloc(mLength);
33+
memcpy(mData, rhs.mData, mLength);
34+
return *this;
35+
}
36+
37+
Buffer
38+
Buffer::copy(const char* data, unsigned int length) {
39+
char* copyData = (char*) malloc(length);
40+
memcpy(copyData, data, length);
41+
return Buffer(copyData, length);
42+
}
43+
44+
const char
45+
Buffer::operator[](unsigned int index) const {
46+
return mData[index];
47+
}
48+
49+
const char*
50+
Buffer::getData() const {
51+
return (const char*) mData;
52+
}
53+
54+
unsigned int
55+
Buffer::getLength() const {
56+
return mLength;
57+
}
58+
59+
void
60+
Buffer::append(const Buffer& other) {
61+
if (mData && mLength > 0) {
62+
if (other.mData && other.mLength > 0) {
63+
char* data = (char*) malloc(mLength + other.mLength);
64+
memcpy(data, mData, mLength);
65+
memcpy(data + mLength, other.mData, other.mLength);
66+
free(mData);
67+
mData = data;
68+
mLength += other.mLength;
69+
}
70+
} else if (other.mData && other.mLength > 0) {
71+
char* data = (char*) malloc(other.mLength);
72+
memcpy(data, other.mData, other.mLength);
73+
74+
if (mData) {
75+
free(mData);
76+
}
77+
78+
mData = data;
79+
mLength += other.mLength;
80+
}
81+
}
82+
83+
Buffer
84+
Buffer::subbuffer(unsigned int start) const {
85+
return subbuffer(start, mLength - start);
86+
}
87+
88+
Buffer
89+
Buffer::subbuffer(unsigned int start, unsigned int length) const {
90+
return Buffer(mData + start, length);
91+
}
92+
93+
bool
94+
Buffer::startsWith(const string str) const {
95+
string dataStr(mData, mLength);
96+
return dataStr.find(str) == 0;
97+
}
98+
99+
int
100+
Buffer::indexOf(const string str) const {
101+
char* pos = (char*) memmem(mData, mLength, str.c_str(), str.length());
102+
return pos - mData;
103+
}

src/buffer.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef __BUFFFER_H__
2+
#define __BUFFFER_H__
3+
4+
#include <string>
5+
6+
using namespace std;
7+
8+
class Buffer {
9+
public:
10+
Buffer(const char* data, unsigned int length);
11+
Buffer(const Buffer& other);
12+
virtual ~Buffer();
13+
14+
Buffer& operator=(const Buffer& rhs);
15+
const char operator[](unsigned int index) const;
16+
17+
static Buffer copy(const char* data, unsigned int length);
18+
19+
const char* getData() const;
20+
unsigned int getLength() const;
21+
22+
void append(const Buffer& other);
23+
Buffer subbuffer(unsigned int start) const;
24+
Buffer subbuffer(unsigned int start, unsigned int length) const;
25+
bool startsWith(const string str) const;
26+
int indexOf(const string str) const;
27+
28+
private:
29+
char* mData;
30+
unsigned int mLength;
31+
};
32+
33+
#endif /* __BUFFFER_H__ */

src/main.cc

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#endif /* HAVE_CONFIG_H */
1111

1212
#include "args.h"
13+
#include "buffer.h"
1314
#include "commparty.h"
1415
#include "print.h"
1516
#include "tcp.h"
@@ -47,11 +48,11 @@ int isIncomingIpPacket(const RawIpPacket* ip) {
4748

4849
#ifdef ENABLE_JSON
4950

50-
bool parseAndPrintJson(const char* data, uint16_t len) {
51+
bool parseAndPrintJson(const Buffer& buffer) {
5152
GError* error = NULL;
5253
bool result = false;
5354
JsonParser* parser = json_parser_new();
54-
json_parser_load_from_data(parser, data, len, &error);
55+
json_parser_load_from_data(parser, buffer.getData(), buffer.getLength(), &error);
5556

5657
if (error) {
5758
g_error_free(error);
@@ -93,8 +94,9 @@ void printTimestamp(struct timeval tv) {
9394
printf("%02d:%02d:%02d.%06ld ", hours, minutes, seconds, microSeconds);
9495
}
9596

96-
void printHttpRequestTitle(const char* data, int /* len */) {
97+
void printHttpRequestTitle(const Buffer& buffer) {
9798
printf("ht ");
99+
const char* data = buffer.getData();
98100
const char* eol_char = strchr(data, '\r');
99101

100102
if (!eol_char) {
@@ -107,13 +109,16 @@ void printHttpRequestTitle(const char* data, int /* len */) {
107109
printf("\n");
108110
}
109111

110-
void handleHttpRequest(const char* data, int len) {
112+
void handleHttpRequest(const Buffer& buffer) {
113+
const char* data = buffer.getData();
114+
int len = buffer.getLength();
115+
111116
if (len > 10 && strncmp(data, "GET ", 4) == 0) {
112-
printHttpRequestTitle(data, len);
117+
printHttpRequestTitle(buffer);
113118
} else if (len > 10 && strncmp(data, "POST ", 5) == 0) {
114-
printHttpRequestTitle(data, len);
119+
printHttpRequestTitle(buffer);
115120
} else if (len > 10 && strncmp(data, "PUT ", 4) == 0) {
116-
printHttpRequestTitle(data, len);
121+
printHttpRequestTitle(buffer);
117122
} else {
118123
printf("ht DATA\n");
119124
}
@@ -122,11 +127,11 @@ void handleHttpRequest(const char* data, int len) {
122127
printf("\n");
123128

124129
#ifdef ENABLE_JSON
125-
if (!parseAndPrintJson(data, len)) {
130+
if (!parseAndPrintJson(buffer)) {
126131
#endif /* ENABLE_JSON */
127-
printIndented(4, data, len);
132+
printIndented(4, buffer);
128133

129-
if (data[len - 1] != '\n') {
134+
if (buffer[len - 1] != '\n') {
130135
printf("\n");
131136
}
132137
#ifdef ENABLE_JSON
@@ -137,15 +142,17 @@ void handleHttpRequest(const char* data, int len) {
137142
}
138143
}
139144

140-
void handleHttpResponse(const char* data, int len) {
141-
if (len > 10 && strncmp(data, "HTTP/1.1", 8) == 0) {
142-
printHttpRequestTitle(data, len);
145+
void handleHttpResponse(const Buffer& buffer) {
146+
if (buffer.startsWith("HTTP/1.1")) {
147+
printHttpRequestTitle(buffer);
143148
} else {
144149
printf("DATA\n");
145150
}
146151

147152
if (!sArgs.useShortOutputFormat()) {
148153
printf("\n");
154+
const char* data = buffer.getData();
155+
int len = buffer.getLength();
149156
const char* bodySeparator = strstr(data, "\r\n\r\n");
150157

151158
if (bodySeparator) {
@@ -155,11 +162,12 @@ void handleHttpResponse(const char* data, int len) {
155162

156163
if (bodyLength > 0) {
157164
const char* body = bodySeparator + 4;
165+
Buffer buffer(body, bodyLength);
158166

159167
#ifdef ENABLE_JSON
160-
if (!parseAndPrintJson(body, bodyLength)) {
168+
if (!parseAndPrintJson(buffer)) {
161169
#endif /* ENABLE_JSON */
162-
printIndented(4, body, bodyLength);
170+
printIndented(4, buffer);
163171
#ifdef ENABLE_JSON
164172
}
165173
#endif /* ENABLE_JSON */
@@ -179,36 +187,38 @@ void printPacketInfo(string partyName, bool isIncoming, struct timeval tv) {
179187
printTimestamp(tv);
180188
}
181189

182-
void handleWebsocketNotification(string partyName, bool isIncoming, struct timeval tv, WebSocketParser* ws, const char* data, uint16_t len) {
190+
void handleWebsocketNotification(string partyName, bool isIncoming, struct timeval tv, WebSocketParser* ws, const Buffer& data) {
183191
WebSocketFrame* frame;
184-
ws->addStreamData(data, len);
192+
ws->addStreamData(data);
185193

186194
while ((frame = ws->getNextFrame()) != nullptr) {
187195
printPacketInfo(partyName, isIncoming, tv);
188196
printf("ws %s\n", frame->getSubject().c_str());
189197

190198
if (!sArgs.useShortOutputFormat()) {
199+
Buffer frameData = frame->getData();
200+
191201
if (frame->getType() == TEXT) {
192202
printf("\n");
193203

194-
if (frame->getDataLength() > 0) {
204+
if (frameData.getLength() > 0) {
195205
#ifdef ENABLE_JSON
196-
if (!parseAndPrintJson(frame->getData(), frame->getDataLength())) {
206+
if (!parseAndPrintJson(frameData)) {
197207
printf(" ");
198-
PRINT_BUFFER(frame->getData(), frame->getDataLength());
208+
PRINT_BUFFER_1(frameData);
199209
printf(" (FAILED TO PARSE)\n");
200210
}
201211
#else /* ENABLE_JSON */
202212
printf(" ");
203-
PRINT_BUFFER(frame->getData(), frame->getDataLength());
213+
PRINT_BUFFER_1(frameData);
204214
printf("\n");
205215
#endif /* ENABLE_JSON */
206216
} else {
207217
printf(" Empty frame\n");
208218
}
209-
} else if (frame->getDataLength() > 0) {
219+
} else if (frameData.getLength() > 0) {
210220
printf("\n");
211-
printIndented(4, frame->getData(), frame->getDataLength());
221+
printIndented(4, frameData.getData(), frameData.getLength());
212222
}
213223

214224
printf("\n");
@@ -229,8 +239,8 @@ bool isWebSocketPort(unsigned short port) {
229239
}
230240

231241
void handleTcpPacket(struct timeval tv, const RawIpPacket* ip, const RawTcpPacket* tcp) {
232-
const char* tcpData = ((const char*) tcp) + tcp->th_off * 4;
233242
uint16_t tcpDataLen = ntohs(ip->ip_len) - sizeof(RawIpPacket) - tcp->th_off * 4;
243+
Buffer buffer(((const char*) tcp) + tcp->th_off * 4, tcpDataLen);
234244

235245
if (!isPacketAllowedByFilters(ip)) {
236246
return;
@@ -249,14 +259,14 @@ void handleTcpPacket(struct timeval tv, const RawIpPacket* ip, const RawTcpPacke
249259

250260
if (isWebSocketPort(src.getPort()) || isWebSocketPort(dest.getPort())) {
251261
WebSocketParser* parser = isIncoming ? party->getWebSocketParserIncoming() : party->getWebSocketParserOutgoing();
252-
handleWebsocketNotification(partyName, isIncoming, tv, parser, tcpData, tcpDataLen);
262+
handleWebsocketNotification(partyName, isIncoming, tv, parser, buffer);
253263
} else if (isHttpPort(src.getPort()) || isHttpPort(dest.getPort())) {
254264
printPacketInfo(partyName, isIncoming, tv);
255265

256266
if (isIncoming) {
257-
handleHttpResponse(tcpData, tcpDataLen);
267+
handleHttpResponse(buffer);
258268
} else {
259-
handleHttpRequest(tcpData, tcpDataLen);
269+
handleHttpRequest(buffer);
260270
}
261271
}
262272

src/print.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ void printIndented(int indent, const char* str, int len) {
5353
}
5454
}
5555

56+
void printIndented(int indent, const Buffer& buffer) {
57+
printIndented(indent, buffer.getData(), buffer.getLength());
58+
}
59+
5660
#ifdef ENABLE_JSON
5761

5862
void printJsonArrayContent(JsonArray*, guint, JsonNode*, gpointer);

src/print.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
#ifndef __JSONPRINT_H__
22
#define __JSONPRINT_H__
33

4+
#include "buffer.h"
5+
6+
#define PRINT_BUFFER_1(buffer) { \
7+
PRINT_BUFFER(buffer.getData(), buffer.getLength()) \
8+
}
9+
410
#define PRINT_BUFFER(data, len) { \
511
int buflen = len; \
612
if (buflen < 0) buflen = 0; \
713
char* buffer = (char*) malloc(buflen + 1); \
8-
buffer[buflen] = '\0'; \
9-
strncpy(buffer, data, buflen); \
14+
buffer[buflen] = '\0'; \
15+
strncpy(buffer, data, buflen); \
1016
printf("%s", buffer); \
1117
free(buffer); \
1218
}
1319

1420
void printIndent(int indent);
1521
void printIndented(int indent, const char* str, int len);
22+
void printIndented(int indent, const Buffer& buffer);
1623

1724
#ifdef ENABLE_JSON
1825

0 commit comments

Comments
 (0)