Skip to content

Commit 8c99466

Browse files
committed
Memory align, TCP assemble
- Remove some compiler flags - `Allocator`: - Fix memory alignment - Rename private sub-class to `_Buffer` to not mix it up with other `Buffer` class - `Row`: replace `ROW_DUMMY_SIZE` with size of a pointer - TCP Stream: - Fix `Stream_id` class, was leaking memory left and right - Add cleanup code in `Stream` - Make sure we got data for DNS length
1 parent 66584e6 commit 8c99466

File tree

3 files changed

+30
-38
lines changed

3 files changed

+30
-38
lines changed

src/Makefile.am

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ SUBDIRS = test
2525
AM_CXXFLAGS = -I$(srcdir) \
2626
-I$(srcdir)/Murmur \
2727
-I$(top_srcdir) \
28-
$(libmaxminddb_CFLAGS) \
29-
-std=c++0x \
30-
-Wall -Wno-parentheses -Wno-switch -Wno-sign-compare -Wno-char-subscripts
28+
$(libmaxminddb_CFLAGS)
3129

3230
bin_PROGRAMS = packetq
3331

src/sql.h

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class Allocator {
216216

217217
void add_buffer()
218218
{
219-
m_curr_buffer = new Buffer(*this);
219+
m_curr_buffer = new _Buffer(*this);
220220
m_buffers.push_back(m_curr_buffer);
221221
}
222222
T* allocate()
@@ -243,28 +243,30 @@ class Allocator {
243243
}
244244
void deallocate(T* item)
245245
{
246-
Buffer** buffptr = (Buffer**)item;
246+
_Buffer** buffptr = (_Buffer**)item;
247247
buffptr[-1]->deallocate(item);
248248
}
249249

250250
private:
251-
class Buffer {
251+
class _Buffer {
252252
private:
253-
Buffer& operator=(const Buffer& other);
254-
Buffer(Buffer&& other) noexcept;
255-
Buffer const& operator=(Buffer&& other);
253+
_Buffer& operator=(const _Buffer& other);
254+
_Buffer(_Buffer&& other) noexcept;
255+
_Buffer const& operator=(_Buffer&& other);
256256

257257
public:
258258
friend class Allocator;
259-
Buffer(Allocator& allocator)
259+
_Buffer(Allocator& allocator)
260260
: m_allocator(allocator)
261261
{
262262
m_has_space = true;
263263
m_used = 0;
264-
m_stride = (sizeof(Buffer*) + m_allocator.m_size);
265-
m_memory = (char*)calloc(m_stride, m_allocator.m_buffersize);
264+
m_stride = (sizeof(_Buffer*) + m_allocator.m_size);
265+
// align size of m_stride to that of a pointer
266+
m_stride = ((m_stride / sizeof(void*)) + 1) * sizeof(void*);
267+
m_memory = (char*)calloc(m_stride, m_allocator.m_buffersize);
266268
}
267-
~Buffer()
269+
~_Buffer()
268270
{
269271
free(m_memory);
270272
}
@@ -277,10 +279,10 @@ class Allocator {
277279
m_free_list.pop();
278280
}
279281
if (!obj && m_used < m_allocator.m_buffersize) {
280-
char* ptr = &m_memory[m_stride * m_used++];
281-
Buffer** b = (Buffer**)ptr;
282-
*b = this;
283-
obj = (T*)(&b[1]);
282+
char* ptr = &m_memory[m_stride * m_used++];
283+
_Buffer** b = (_Buffer**)ptr;
284+
*b = this;
285+
obj = (T*)(&b[1]);
284286
}
285287
m_has_space = true;
286288
if (!obj)
@@ -302,8 +304,8 @@ class Allocator {
302304
char* m_memory;
303305
};
304306

305-
Buffer* m_curr_buffer;
306-
std::list<Buffer*> m_buffers;
307+
_Buffer* m_curr_buffer;
308+
std::list<_Buffer*> m_buffers;
307309

308310
int m_buffersize;
309311
int m_size;
@@ -452,7 +454,7 @@ class Table {
452454
std::vector<int> m_text_column_offsets;
453455
};
454456

455-
#define ROW_DUMMY_SIZE 4
457+
#define ROW_DUMMY_SIZE sizeof(void*)
456458

457459
class Row {
458460
public:

src/tcp.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@ namespace packetq {
3333
class Stream_id {
3434
public:
3535
/// constructor
36-
Stream_id()
37-
: m_src_port(0)
38-
, m_dst_port(0)
39-
{
40-
memset(&m_src_ip, 0, sizeof(m_src_ip));
41-
memset(&m_dst_ip, 0, sizeof(m_dst_ip));
42-
}
43-
/// constructor taking source and destination adresses
4436
Stream_id(in6addr_t& src_ip,
4537
in6addr_t& dst_ip,
4638
unsigned short src_port,
@@ -55,15 +47,7 @@ class Stream_id {
5547
/// < comparison operator for the std::map
5648
bool operator<(const Stream_id& rhs) const
5749
{
58-
if (memcmp(&m_src_ip.__in6_u.__u6_addr8, &rhs.m_src_ip.__in6_u.__u6_addr8, sizeof(m_src_ip.__in6_u.__u6_addr8)) < 0)
59-
return true;
60-
if (memcmp(&m_dst_ip.__in6_u.__u6_addr8, &rhs.m_dst_ip.__in6_u.__u6_addr8, sizeof(m_dst_ip.__in6_u.__u6_addr8)) < 0)
61-
return true;
62-
if (m_src_port < rhs.m_src_port)
63-
return true;
64-
if (m_dst_port < rhs.m_dst_port)
65-
return true;
66-
return false;
50+
return memcmp(this, &rhs, sizeof(*this)) < 0;
6751
}
6852

6953
private:
@@ -128,6 +112,10 @@ class Stream {
128112
m_nseq = false;
129113
m_seq = 0;
130114
}
115+
~Stream()
116+
{
117+
m_segments.clear();
118+
}
131119
/// add a datasegment to the stream
132120
/** If the segment has the expected sequence number
133121
* the segment will be added to the list
@@ -255,7 +243,11 @@ assemble_tcp(
255243

256244
data = 0;
257245
if (str.has_content()) {
258-
int size = str.get_size();
246+
int size = str.get_size();
247+
if (size < 2) {
248+
// need at least dnslen
249+
return 0;
250+
}
259251
unsigned char* buffer = str.get_buffer();
260252
int dns_size = (int(buffer[0]) << 8) | buffer[1];
261253

0 commit comments

Comments
 (0)