diff options
Diffstat (limited to 'analyzer/utils')
| -rw-r--r-- | analyzer/utils/fs.c | 9 | ||||
| -rw-r--r-- | analyzer/utils/memory.c | 24 | ||||
| -rw-r--r-- | analyzer/utils/memory.h | 26 | ||||
| -rw-r--r-- | analyzer/utils/tcp.c | 26 | ||||
| -rw-r--r-- | analyzer/utils/tcp.h | 5 |
5 files changed, 20 insertions, 70 deletions
diff --git a/analyzer/utils/fs.c b/analyzer/utils/fs.c index 5e3eb1c..e6d3256 100644 --- a/analyzer/utils/fs.c +++ b/analyzer/utils/fs.c @@ -1,5 +1,6 @@ #include "utils/fs.h" +#include <limits.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -10,25 +11,25 @@ void get_home_dir(char *home_dir) { } void get_main_dir(char *main_dir) { - char home_dir[256]; + char home_dir[PATH_MAX]; get_home_dir(home_dir); sprintf(main_dir, "%s/.local/share/branal", home_dir); } void get_capture_dir(char *capture_dir) { - char main_dir[256]; + char main_dir[PATH_MAX]; get_main_dir(main_dir); sprintf(capture_dir, "%s/captures", main_dir); } void get_saves_dir(char *saves_dir) { - char main_dir[256]; + char main_dir[PATH_MAX]; get_main_dir(main_dir); sprintf(saves_dir, "%s/saves", main_dir); } void create_necessary_directories() { - char main_dir[256], capture_dir[256], saves_dir[256]; + char main_dir[PATH_MAX], capture_dir[PATH_MAX], saves_dir[PATH_MAX]; get_main_dir(main_dir); get_capture_dir(capture_dir); get_saves_dir(saves_dir); diff --git a/analyzer/utils/memory.c b/analyzer/utils/memory.c index 91c5f18..3e29671 100644 --- a/analyzer/utils/memory.c +++ b/analyzer/utils/memory.c @@ -2,7 +2,6 @@ #include <stdio.h> #include <stdlib.h> -#include <string.h> #include <unistd.h> void *safe_malloc(size_t size) { @@ -37,26 +36,3 @@ void *safe_realloc(void *ptr, size_t size) { fprintf(stderr, "FATAL: Failed realloc %d times. Exiting...", MAX_ALLOC_ATTEMPTS); exit(EXIT_FAILURE); } - -// expand an array -// returns pointer to the expanded array -void *array_expand(void *arr, size_t el_size, size_t *cap, size_t to_fit) { - while (*cap < to_fit) - *cap *= 2; - return safe_realloc(arr, *cap * el_size); -} - -void *array_append(void *arr, size_t el_size, size_t *len, size_t *cap, void *val) { - if (*len >= *cap) - arr = array_expand(arr, el_size, cap, *len + 1); - memcpy((char *)arr + (*len)++ * el_size, val, el_size); - return arr; -} - -void *array_concat(void *arr1, size_t el_size, size_t *len1, size_t *cap, void *arr2, size_t len2) { - if (*len1 + len2 > *cap) - arr1 = array_expand(arr1, el_size, cap, *len1 + len2); - memcpy((char *)arr1 + *len1 * el_size, arr2, len2 * el_size); - *len1 += len2; - return arr1; -} diff --git a/analyzer/utils/memory.h b/analyzer/utils/memory.h index 0973623..8f01ba4 100644 --- a/analyzer/utils/memory.h +++ b/analyzer/utils/memory.h @@ -3,6 +3,9 @@ #include <stddef.h> +#ifndef MEMORY_H +#define MEMORY_H + #define ARR_LEN(arr) (sizeof(arr) / sizeof(arr[0])) #define MAX_ALLOC_ATTEMPTS 3 @@ -22,25 +25,4 @@ extern void *safe_malloc(size_t size); // pointer to the reallocated memory extern void *safe_realloc(void *ptr, size_t size); -// append to array, expand if necessary -// params: -// - arr - array to append to -// - el_size - size of an array element -// - len - pointer to the length of the array -// - cap - pointer to the capacity of the array -// - val - pointer to the value to append to the array -// returns: -// new pointer to the array (different if it was realloced) -extern void *array_append(void *arr, size_t el_size, size_t *len, size_t *cap, void *val); - -// concatenate 2 arrays, expand if necessary -// params: -// - arr1 - first array (destination) -// - el_size - size of an array element -// - len1 - pointer to the length of arr1 -// - cap - pointer to the capacity of arr1 -// - arr2 - second array -// - len2 - length of arr2 -// returns: -// new pointer to the array (different if it was realloced) -extern void *array_concat(void *arr1, size_t el_size, size_t *len1, size_t *cap, void *arr2, size_t len2); +#endif diff --git a/analyzer/utils/tcp.c b/analyzer/utils/tcp.c index 7a79c67..add2a76 100644 --- a/analyzer/utils/tcp.c +++ b/analyzer/utils/tcp.c @@ -77,9 +77,7 @@ TCPSegment get_segment_from_packet(const byte_t *packet, const struct pcap_pkthd TCPStream new_stream(ConnectionSource src) { // clang-format off TCPStream stream = { - .data = safe_malloc(INIT_STREAM_CAPACITY), - .len = 0, - .capacity = INIT_STREAM_CAPACITY, + .bytes = array_new(1, INIT_STREAM_CAPACITY, NULL), .seq = 0, .port = 0, .src = src, @@ -90,13 +88,12 @@ TCPStream new_stream(ConnectionSource src) { } void destroy_stream(TCPStream stream) { - free(stream.data); + array_destroy(stream.bytes); heap_free(stream.pending); } void remove_parsed_data(TCPStream *stream, size_t parsed_len) { - memmove(stream->data, stream->data + parsed_len, stream->len - parsed_len); - stream->len -= parsed_len; + array_remove_front(&stream->bytes, parsed_len); stream->seq += parsed_len; } @@ -104,7 +101,7 @@ void remove_parsed_data(TCPStream *stream, size_t parsed_len) { // return the index of the first relevant byte in payload or -1 if segment isn't directly connected to stream int get_first_relevant_byte_index(TCPStream stream, TCPSegment segment) { // expected tcp sequence numbers for stream and segment - uint32_t stream_expected = stream.seq + stream.len; + uint32_t stream_expected = stream.seq + stream.bytes.count; uint32_t segment_expected = segment.seq + segment.len; if (seq_in_range_ie(stream_expected, segment.seq, segment_expected)) return stream_expected - segment.seq; @@ -114,16 +111,11 @@ int get_first_relevant_byte_index(TCPStream stream, TCPSegment segment) { // check if the segment is in acceptable range to the stream int in_acceptable_range(TCPStream stream, TCPSegment segment) { // expected tcp sequence number for stream and max acceptable sequence number for segment - uint32_t stream_expected = stream.seq + stream.len; + uint32_t stream_expected = stream.seq + stream.bytes.count; uint32_t max_acceptable_seq = stream_expected + MAX_SEQ_OFFSET; return seq_in_range_ei(segment.seq, stream_expected, max_acceptable_seq); } -// add segment directly to stream -void add_segment(TCPStream *stream, TCPSegment segment, size_t data_start) { - stream->data = array_concat(stream->data, 1, &stream->len, &stream->capacity, segment.payload + data_start, segment.len - data_start); -} - // add pending segments to the stream void add_pending_segments(TCPStream *stream) { int top_changed = 1; @@ -134,7 +126,7 @@ void add_pending_segments(TCPStream *stream) { // add pending segment it it became directly connected int data_start = get_first_relevant_byte_index(*stream, *top); if (data_start != -1) { - add_segment(stream, *top, data_start); + array_concat(&stream->bytes, top->payload + data_start, top->len - data_start); top_changed = 1; } @@ -152,11 +144,11 @@ int handle_segment(TCPStream *stream, TCPSegment segment) { heap_free(stream->pending); stream->pending = heap_new(sizeof(TCPSegment), segments_compare, segment_cleanup); - stream->len = 0; + array_remove_front(&stream->bytes, stream->bytes.count); stream->seq = segment.seq; stream->port = segment.port; - add_segment(stream, segment, 0); + array_concat(&stream->bytes, segment.payload, segment.len); free(segment.payload); return 1; } @@ -164,7 +156,7 @@ int handle_segment(TCPStream *stream, TCPSegment segment) { // add segment directly to the stream if it's directly connected int data_start = get_first_relevant_byte_index(*stream, segment); if (data_start != -1) { - add_segment(stream, segment, data_start); + array_concat(&stream->bytes, segment.payload + data_start, segment.len - data_start); free(segment.payload); add_pending_segments(stream); return 1; diff --git a/analyzer/utils/tcp.h b/analyzer/utils/tcp.h index 382c585..55cbf3e 100644 --- a/analyzer/utils/tcp.h +++ b/analyzer/utils/tcp.h @@ -4,6 +4,7 @@ #include <pcap.h> #include "structures/heap.h" +#include "structures/array.h" #ifndef TCP_H #define TCP_H @@ -32,9 +33,7 @@ extern TCPSegment get_segment_from_packet(const byte_t *packet, const struct pca // struct that stores all the information about stream typedef struct { - byte_t *data; // bytes in the stream - size_t len; // length of the stream - size_t capacity; // capacity of the data buffer + DynArray bytes; // bytes in the stream uint32_t seq; // TCP sequence number of the first byte in the stream uint16_t port; // client port of currently analyzed connection ConnectionSource src; // stream's source |
