Skip to content

Commit

Permalink
Memory align, TCP assemble
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
jelu committed Sep 4, 2024
1 parent 66584e6 commit 8c99466
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 38 deletions.
4 changes: 1 addition & 3 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ SUBDIRS = test
AM_CXXFLAGS = -I$(srcdir) \
-I$(srcdir)/Murmur \
-I$(top_srcdir) \
$(libmaxminddb_CFLAGS) \
-std=c++0x \
-Wall -Wno-parentheses -Wno-switch -Wno-sign-compare -Wno-char-subscripts
$(libmaxminddb_CFLAGS)

bin_PROGRAMS = packetq

Expand Down
36 changes: 19 additions & 17 deletions src/sql.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class Allocator {

void add_buffer()
{
m_curr_buffer = new Buffer(*this);
m_curr_buffer = new _Buffer(*this);
m_buffers.push_back(m_curr_buffer);
}
T* allocate()
Expand All @@ -243,28 +243,30 @@ class Allocator {
}
void deallocate(T* item)
{
Buffer** buffptr = (Buffer**)item;
_Buffer** buffptr = (_Buffer**)item;
buffptr[-1]->deallocate(item);
}

private:
class Buffer {
class _Buffer {
private:
Buffer& operator=(const Buffer& other);
Buffer(Buffer&& other) noexcept;
Buffer const& operator=(Buffer&& other);
_Buffer& operator=(const _Buffer& other);
_Buffer(_Buffer&& other) noexcept;
_Buffer const& operator=(_Buffer&& other);

public:
friend class Allocator;
Buffer(Allocator& allocator)
_Buffer(Allocator& allocator)
: m_allocator(allocator)
{
m_has_space = true;
m_used = 0;
m_stride = (sizeof(Buffer*) + m_allocator.m_size);
m_memory = (char*)calloc(m_stride, m_allocator.m_buffersize);
m_stride = (sizeof(_Buffer*) + m_allocator.m_size);
// align size of m_stride to that of a pointer
m_stride = ((m_stride / sizeof(void*)) + 1) * sizeof(void*);
m_memory = (char*)calloc(m_stride, m_allocator.m_buffersize);
}
~Buffer()
~_Buffer()
{
free(m_memory);
}
Expand All @@ -277,10 +279,10 @@ class Allocator {
m_free_list.pop();
}
if (!obj && m_used < m_allocator.m_buffersize) {
char* ptr = &m_memory[m_stride * m_used++];
Buffer** b = (Buffer**)ptr;
*b = this;
obj = (T*)(&b[1]);
char* ptr = &m_memory[m_stride * m_used++];
_Buffer** b = (_Buffer**)ptr;
*b = this;
obj = (T*)(&b[1]);
}
m_has_space = true;
if (!obj)
Expand All @@ -302,8 +304,8 @@ class Allocator {
char* m_memory;
};

Buffer* m_curr_buffer;
std::list<Buffer*> m_buffers;
_Buffer* m_curr_buffer;
std::list<_Buffer*> m_buffers;

int m_buffersize;
int m_size;
Expand Down Expand Up @@ -452,7 +454,7 @@ class Table {
std::vector<int> m_text_column_offsets;
};

#define ROW_DUMMY_SIZE 4
#define ROW_DUMMY_SIZE sizeof(void*)

class Row {
public:
Expand Down
28 changes: 10 additions & 18 deletions src/tcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ namespace packetq {
class Stream_id {
public:
/// constructor
Stream_id()
: m_src_port(0)
, m_dst_port(0)
{
memset(&m_src_ip, 0, sizeof(m_src_ip));
memset(&m_dst_ip, 0, sizeof(m_dst_ip));
}
/// constructor taking source and destination adresses
Stream_id(in6addr_t& src_ip,
in6addr_t& dst_ip,
unsigned short src_port,
Expand All @@ -55,15 +47,7 @@ class Stream_id {
/// < comparison operator for the std::map
bool operator<(const Stream_id& rhs) const
{
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)
return true;
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)
return true;
if (m_src_port < rhs.m_src_port)
return true;
if (m_dst_port < rhs.m_dst_port)
return true;
return false;
return memcmp(this, &rhs, sizeof(*this)) < 0;
}

private:
Expand Down Expand Up @@ -128,6 +112,10 @@ class Stream {
m_nseq = false;
m_seq = 0;
}
~Stream()
{
m_segments.clear();
}
/// add a datasegment to the stream
/** If the segment has the expected sequence number
* the segment will be added to the list
Expand Down Expand Up @@ -255,7 +243,11 @@ assemble_tcp(

data = 0;
if (str.has_content()) {
int size = str.get_size();
int size = str.get_size();
if (size < 2) {
// need at least dnslen
return 0;
}
unsigned char* buffer = str.get_buffer();
int dns_size = (int(buffer[0]) << 8) | buffer[1];

Expand Down

0 comments on commit 8c99466

Please sign in to comment.