aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYunchih Chen <yunchih.cat@gmail.com>2018-03-05 10:57:00 +0800
committerYunchih Chen <yunchih.cat@gmail.com>2018-03-05 10:57:00 +0800
commita3fd587346f4f1b8868b0a147c561c55b1b1da38 (patch)
tree7c78ae724e03904fabd5d16c56090baf1c615de3
parentdd91a91704b76c3f46b841d86f5cedee684e7fa9 (diff)
downloadnfcollect-a3fd587346f4f1b8868b0a147c561c55b1b1da38.tar
nfcollect-a3fd587346f4f1b8868b0a147c561c55b1b1da38.tar.gz
nfcollect-a3fd587346f4f1b8868b0a147c561c55b1b1da38.tar.bz2
nfcollect-a3fd587346f4f1b8868b0a147c561c55b1b1da38.tar.lz
nfcollect-a3fd587346f4f1b8868b0a147c561c55b1b1da38.tar.xz
nfcollect-a3fd587346f4f1b8868b0a147c561c55b1b1da38.tar.zst
nfcollect-a3fd587346f4f1b8868b0a147c561c55b1b1da38.zip
Apply clang-format changes
-rw-r--r--bin/nfcollect.c11
-rw-r--r--bin/nfextract.c35
-rw-r--r--include/collect.h3
-rw-r--r--include/commit.h3
-rw-r--r--include/common.h5
-rw-r--r--include/main.h124
-rw-r--r--lib/collect.c55
-rw-r--r--lib/commit.c36
-rw-r--r--lib/common.c103
-rw-r--r--lib/extract.c27
10 files changed, 202 insertions, 200 deletions
diff --git a/bin/nfcollect.c b/bin/nfcollect.c
index 10ad512..85645a4 100644
--- a/bin/nfcollect.c
+++ b/bin/nfcollect.c
@@ -23,9 +23,9 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
+#include "collect.h"
#include "commit.h"
#include "common.h"
-#include "collect.h"
#include <fcntl.h>
#include <getopt.h>
#include <pthread.h>
@@ -71,7 +71,8 @@ int main(int argc, char *argv[]) {
{0, 0, 0, 0}};
int opt;
- while ((opt = getopt_long(argc, argv, "c:g:d:s:hv", longopts, NULL)) != -1) {
+ while ((opt = getopt_long(argc, argv, "c:g:d:s:hv", longopts, NULL)) !=
+ -1) {
switch (opt) {
case 'h':
printf("%s", help_text);
@@ -130,10 +131,12 @@ int main(int argc, char *argv[]) {
sem_init(g.nfl_commit_queue, 0, max_commit_worker);
// Set up nflog receiver worker
- nflog_state_t **trunks = (nflog_state_t **)calloc(trunk_cnt, sizeof(void*));
+ nflog_state_t **trunks =
+ (nflog_state_t **)calloc(trunk_cnt, sizeof(void *));
nfl_commit_init(trunk_cnt);
- debug("Worker started, entries_max = %d, trunk_cnt = %d", entries_max, trunk_cnt);
+ debug("Worker started, entries_max = %d, trunk_cnt = %d", entries_max,
+ trunk_cnt);
for (i = 0;; i = NEXT(i, trunk_cnt)) {
debug("Running receiver worker: id = %d", i);
nfl_state_init(&(trunks[i]), i, entries_max, &g);
diff --git a/bin/nfextract.c b/bin/nfextract.c
index 884665a..00b285e 100644
--- a/bin/nfextract.c
+++ b/bin/nfextract.c
@@ -22,8 +22,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
-#include "extract.h"
#include "common.h"
+#include "extract.h"
#include <dirent.h>
#include <fcntl.h>
#include <getopt.h>
@@ -58,45 +58,46 @@ static void sig_handler(int signo) {
static void extract_each(const char *filename) {
nflog_state_t trunk;
- if(nfl_extract_worker(filename, &trunk) < 0)
+ if (nfl_extract_worker(filename, &trunk) < 0)
return;
char output[1024];
- for(int entry = 0; entry < trunk.header->n_entries; ++entry){
+ for (int entry = 0; entry < trunk.header->n_entries; ++entry) {
nfl_format_output(output, trunk.store);
- puts((char*)output);
- free((char*)output);
+ puts((char *)output);
+ free((char *)output);
}
- free((char*)filename);
+ free((char *)filename);
}
static void extract_all(const char *storage_dir) {
- DIR *dp;
- struct dirent *ep;
+ DIR *dp;
+ struct dirent *ep;
int i, index, max_index = -1;
char *trunk_files[MAX_TRUNK_ID];
memset(trunk_files, MAX_TRUNK_ID, 0);
- ERR(!(dp = opendir(storage_dir)),
- "Can't open the storage directory");
+ ERR(!(dp = opendir(storage_dir)), "Can't open the storage directory");
while ((ep = readdir(dp))) {
index = nfl_storage_match_index(ep->d_name);
- if(index >= 0) {
- if(index >= MAX_TRUNK_ID) {
+ if (index >= 0) {
+ if (index >= MAX_TRUNK_ID) {
WARN(1, "Storage trunk file index "
- "out of predefined range: %s", ep->d_name);
+ "out of predefined range: %s",
+ ep->d_name);
} else {
trunk_files[index] = strdup(ep->d_name);
- if(index > max_index) max_index = index;
+ if (index > max_index)
+ max_index = index;
}
}
}
- closedir (dp);
+ closedir(dp);
- for(i = 0; i < max_index; ++i)
- if(trunk_files[i])
+ for (i = 0; i < max_index; ++i)
+ if (trunk_files[i])
extract_each(trunk_files[i]);
}
diff --git a/include/collect.h b/include/collect.h
index 98eee4d..d146845 100644
--- a/include/collect.h
+++ b/include/collect.h
@@ -1,5 +1,6 @@
#pragma once
void *nfl_collect_worker(void *targs);
-void nfl_state_init(nflog_state_t **nf, uint32_t id, uint32_t entries_max, nflog_global_t *g);
+void nfl_state_init(nflog_state_t **nf, uint32_t id, uint32_t entries_max,
+ nflog_global_t *g);
void nfl_state_free(nflog_state_t *nf);
diff --git a/include/commit.h b/include/commit.h
index d61fbe2..add18c7 100644
--- a/include/commit.h
+++ b/include/commit.h
@@ -3,6 +3,7 @@
#include "common.h"
void nfl_commit_init();
-void nfl_commit_worker(nflog_header_t* header, nflog_entry_t* store, const char* filename);
+void nfl_commit_worker(nflog_header_t *header, nflog_entry_t *store,
+ const char *filename);
#endif
diff --git a/include/common.h b/include/common.h
index 6030780..bf40ad6 100644
--- a/include/common.h
+++ b/include/common.h
@@ -5,7 +5,8 @@ int nfl_check_file(FILE *f);
int nfl_check_dir(const char *storage_dir);
int nfl_storage_match_index(const char *fn);
const char *nfl_get_filename(const char *dir, int id);
-void nfl_cal_trunk(uint32_t total_size, uint32_t *trunk_cnt, uint32_t *trunk_size);
+void nfl_cal_trunk(uint32_t total_size, uint32_t *trunk_cnt,
+ uint32_t *trunk_size);
void nfl_cal_entries(uint32_t trunk_size, uint32_t *entries_cnt);
-void nfl_format_output(char* output, nflog_entry_t *entry);
+void nfl_format_output(char *output, nflog_entry_t *entry);
int nfl_setup_compression(const char *flag, enum nflog_compression_t *opt);
diff --git a/include/main.h b/include/main.h
index 40995e9..bd4d38c 100644
--- a/include/main.h
+++ b/include/main.h
@@ -24,14 +24,14 @@
#ifndef _MAIN_H
#define _MAIN_H
#include <assert.h>
-#include <semaphore.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
+#include <semaphore.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
#ifdef DEBUG
#define DEBUG_ON 1
@@ -40,102 +40,96 @@
#endif
#define ASSERT(condition, error_msg) \
- if (!(condition)) { \
- fputs((error_msg), stderr); \
- exit(1); \
- }
+ if (!(condition)) { \
+ fputs((error_msg), stderr); \
+ exit(1); \
+ }
#define ERR(command, error_msg) \
- if (command) { \
- perror((error_msg)); \
- exit(1); \
- }
+ if (command) { \
+ perror((error_msg)); \
+ exit(1); \
+ }
#define WARN(command, format, ...) \
- if (command) { \
- fprintf(stdout, format "\n", ##__VA_ARGS__); \
- }
+ if (command) { \
+ fprintf(stdout, format "\n", ##__VA_ARGS__); \
+ }
#define WARN_RETURN(command, format, ...) \
- if (command) { \
- fprintf(stdout, format "\n", ##__VA_ARGS__); \
- return -1; \
- }
+ if (command) { \
+ fprintf(stdout, format "\n", ##__VA_ARGS__); \
+ return -1; \
+ }
#define debug(format, ...) \
- if (DEBUG_ON) { \
- fprintf(stdout, "[DEBUG] " format "\n", ##__VA_ARGS__); \
- }
+ if (DEBUG_ON) { \
+ fprintf(stdout, "[DEBUG] " format "\n", ##__VA_ARGS__); \
+ }
-#define likely(x) __builtin_expect((x),1)
-#define unlikely(x) __builtin_expect((x),0)
+#define likely(x) __builtin_expect((x), 1)
+#define unlikely(x) __builtin_expect((x), 0)
-#define CEIL_DIV(a,b) (((a)+(b) - 1)/(b))
-#define NEXT(i, l) ((i+1) % l)
-#define PREV(i, l) ((i-1) % l)
+#define CEIL_DIV(a, b) (((a) + (b)-1) / (b))
+#define NEXT(i, l) ((i + 1) % l)
+#define PREV(i, l) ((i - 1) % l)
#define TRUNK_SIZE_BY_PAGE (150) // 150 pages
#define MAX_TRUNK_ID (80)
#define STORAGE_PREFIX "nflog_storage"
-enum nflog_compression_t {
- COMPRESS_NONE,
- COMPRESS_LZ4,
- COMPRESS_ZSTD
-};
+enum nflog_compression_t { COMPRESS_NONE, COMPRESS_LZ4, COMPRESS_ZSTD };
typedef struct __attribute__((packed)) _nflog_header_t {
- uint16_t cksum; /* 0 4 */
- enum nflog_compression_t compression_opt; /* 0 4 */
- uint32_t id; /* 4 4 */
- uint32_t n_entries; /* 8 4 */
- uint32_t max_n_entries; /* 12 4 */
- time_t start_time; /* 16 8 */
- time_t end_time; /* 24 8 */
-
- /* size: 32, cachelines: 1, members: 6 */
+ uint16_t cksum; /* 0 4 */
+ enum nflog_compression_t compression_opt; /* 0 4 */
+ uint32_t id; /* 4 4 */
+ uint32_t n_entries; /* 8 4 */
+ uint32_t max_n_entries; /* 12 4 */
+ time_t start_time; /* 16 8 */
+ time_t end_time; /* 24 8 */
+
+ /* size: 32, cachelines: 1, members: 6 */
} nflog_header_t;
-
typedef struct __attribute__((packed)) _nflog_entry_t {
- // current timestamp since UNIX epoch
- time_t timestamp; /* 0 8 */
+ // current timestamp since UNIX epoch
+ time_t timestamp; /* 0 8 */
- // dest address
- struct in_addr daddr; /* 8 4 */
+ // dest address
+ struct in_addr daddr; /* 8 4 */
- // uid
- uint32_t uid; /* 12 4 */
+ // uid
+ uint32_t uid; /* 12 4 */
- // unused space, just for padding
- uint8_t __unused1; /* 16 1 */
+ // unused space, just for padding
+ uint8_t __unused1; /* 16 1 */
- // IP protocol (UDP or TCP)
- uint8_t protocol; /* 17 1 */
+ // IP protocol (UDP or TCP)
+ uint8_t protocol; /* 17 1 */
- // unused space, just for padding
- uint16_t __unused2; /* 18 2 */
+ // unused space, just for padding
+ uint16_t __unused2; /* 18 2 */
- // source port
- uint16_t sport; /* 20 2 */
+ // source port
+ uint16_t sport; /* 20 2 */
- // destination port
- uint16_t dport; /* 22 2 */
+ // destination port
+ uint16_t dport; /* 22 2 */
- /* size: 24, cachelines: 1, members: 8 */
+ /* size: 24, cachelines: 1, members: 8 */
} nflog_entry_t;
-
typedef struct _nflog_global_t {
- sem_t* nfl_commit_queue;
+ sem_t *nfl_commit_queue;
uint16_t nfl_group_id;
- const char* storage_dir;
+ const char *storage_dir;
enum nflog_compression_t compression_opt;
} nflog_global_t;
typedef struct _nflog_state_t {
- nflog_global_t* global;
- nflog_header_t* header;
- nflog_entry_t* store;
+ nflog_global_t *global;
+ nflog_header_t *header;
+ nflog_entry_t *store;
struct nflog_handle *nfl_fd;
struct nflog_g_handle *nfl_group_fd;
diff --git a/lib/collect.c b/lib/collect.c
index faef7b8..0cba852 100644
--- a/lib/collect.c
+++ b/lib/collect.c
@@ -24,11 +24,11 @@
#include "commit.h"
#include "common.h"
-#include <stddef.h> // size_t for libnetfilter_log
-#include <sys/types.h> // u_int32_t for libnetfilter_log
#include <libnetfilter_log/libnetfilter_log.h>
#include <pthread.h>
+#include <stddef.h> // size_t for libnetfilter_log
#include <string.h>
+#include <sys/types.h> // u_int32_t for libnetfilter_log
#include <time.h>
nflog_global_t g;
@@ -70,7 +70,7 @@ static int handle_packet(struct nflog_g_handle *gh, struct nfgenmsg *nfmsg,
entry->dport = ntohs(tcph->dest);
// only process SYNC and PSH packet, drop ACK
- if(!tcph->syn && !tcph->psh)
+ if (!tcph->syn && !tcph->psh)
return 1;
} else if (iph->protocol == IPPROTO_UDP) {
udph = (struct udphdr *)inner_hdr;
@@ -98,10 +98,10 @@ static int handle_packet(struct nflog_g_handle *gh, struct nfgenmsg *nfmsg,
"uid:\t%d,\t"
"sport:\t%d,\t"
"dport:\t%d",
- nf->header->n_entries,
- entry->timestamp, (unsigned long)entry->daddr.s_addr,
- iph->protocol == IPPROTO_TCP ? "TCP" : "UDP",
- entry->uid, entry->sport, entry->dport);
+ nf->header->n_entries, entry->timestamp,
+ (unsigned long)entry->daddr.s_addr,
+ iph->protocol == IPPROTO_TCP ? "TCP" : "UDP", entry->uid,
+ entry->sport, entry->dport);
// Ignore IPv6 packet for now Q_Q
return 0;
@@ -118,8 +118,10 @@ static void nfl_init(nflog_state_t *nf) {
// bind to group
nf->nfl_group_fd = nflog_bind_group(nf->nfl_fd, nf->global->nfl_group_id);
- /* ERR(nflog_set_mode(nf->nfl_group_fd, NFULNL_COPY_PACKET, sizeof(struct iphdr) + 4) < 0, */
- ERR(nflog_set_mode(nf->nfl_group_fd, NFULNL_COPY_PACKET, nflog_recv_size) < 0,
+ /* ERR(nflog_set_mode(nf->nfl_group_fd, NFULNL_COPY_PACKET, sizeof(struct
+ * iphdr) + 4) < 0, */
+ ERR(nflog_set_mode(nf->nfl_group_fd, NFULNL_COPY_PACKET, nflog_recv_size) <
+ 0,
"Could not set copy mode");
nflog_callback_register(nf->nfl_group_fd, &handle_packet, nf);
@@ -139,17 +141,19 @@ void *nfl_collect_worker(void *targs) {
debug("Recv worker #%u: main loop starts", nf->header->id);
time(&nf->header->start_time);
- int rv; char buf[4096];
+ int rv;
+ char buf[4096];
while (*p_cnt_now < cnt_max) {
pthread_testcancel(); /* cancellation point */
if ((rv = recv(fd, buf, sizeof(buf), 0)) && rv > 0) {
- debug("Recv worker #%u: nflog packet received (len=%u)", nf->header->id,
- rv);
+ debug("Recv worker #%u: nflog packet received (len=%u)",
+ nf->header->id, rv);
nflog_handle_packet(nf->nfl_fd, buf, rv);
}
}
- debug("Recv worker #%u: finish recv, received packets: %u", nf->header->id, cnt_max);
+ debug("Recv worker #%u: finish recv, received packets: %u", nf->header->id,
+ cnt_max);
// write end time
time(&nf->header->end_time);
@@ -172,7 +176,7 @@ static void nfl_commit(nflog_state_t *nf) {
}
static void *nfl_start_commit_worker(void *targs) {
- nflog_state_t* nf = (nflog_state_t*) targs;
+ nflog_state_t *nf = (nflog_state_t *)targs;
const char *filename = nfl_get_filename(g.storage_dir, nf->header->id);
debug("Comm worker #%u: thread started.", nf->header->id);
@@ -183,7 +187,7 @@ static void *nfl_start_commit_worker(void *targs) {
sem_post(g.nfl_commit_queue);
nfl_state_free(nf);
- free((char*)filename);
+ free((char *)filename);
pthread_mutex_lock(&nf->has_finished_lock);
nf->has_finished = true;
@@ -197,11 +201,10 @@ static void *nfl_start_commit_worker(void *targs) {
* State managers
*/
-void nfl_state_init(nflog_state_t **nf,
- uint32_t id, uint32_t entries_max,
- nflog_global_t *g) {
+void nfl_state_init(nflog_state_t **nf, uint32_t id, uint32_t entries_max,
+ nflog_global_t *g) {
assert(nf);
- if(unlikely(*nf == NULL)) {
+ if (unlikely(*nf == NULL)) {
*nf = (nflog_state_t *)malloc(sizeof(nflog_state_t));
(*nf)->global = g;
(*nf)->header = (nflog_header_t *)malloc(sizeof(nflog_header_t));
@@ -214,11 +217,14 @@ void nfl_state_init(nflog_state_t **nf,
pthread_cond_init(&(*nf)->has_finished_cond, NULL);
}
- // Ensure trunk with same id in previous run has finished to prevent reusing a trunk
- // which it's still being used. Furthermore, this hopefully alleviate us from
+ // Ensure trunk with same id in previous run has finished to prevent reusing
+ // a trunk
+ // which it's still being used. Furthermore, this hopefully alleviate us
+ // from
// bursty network traffic.
pthread_mutex_lock(&(*nf)->has_finished_lock);
- while(!(*nf)->has_finished) pthread_cond_wait(&(*nf)->has_finished_cond, &(*nf)->has_finished_lock);
+ while (!(*nf)->has_finished)
+ pthread_cond_wait(&(*nf)->has_finished_cond, &(*nf)->has_finished_lock);
(*nf)->has_finished = false;
pthread_mutex_unlock(&(*nf)->has_finished_lock);
@@ -226,11 +232,10 @@ void nfl_state_init(nflog_state_t **nf,
// consume physical memory before we fill the buffer.
// Instead, fill entries with 0 on the fly, to squeeze
// more space for compression.
- (*nf)->store = (nflog_entry_t *)malloc(sizeof(nflog_entry_t) *
- entries_max);
+ (*nf)->store = (nflog_entry_t *)malloc(sizeof(nflog_entry_t) * entries_max);
}
static void nfl_state_free(nflog_state_t *nf) {
// Free only and leave the rest intact
- free((void*)nf->store);
+ free((void *)nf->store);
}
diff --git a/lib/commit.c b/lib/commit.c
index 12cc027..202a138 100644
--- a/lib/commit.c
+++ b/lib/commit.c
@@ -1,36 +1,35 @@
+#include "commit.h"
#include <errno.h>
#include <string.h>
#include <zstd.h>
-#include "commit.h"
-static void nfl_commit_default(FILE* f, nflog_entry_t* store, uint32_t store_size);
-static void nfl_commit_lz4(FILE* f, nflog_entry_t* store, uint32_t store_size);
-static void nfl_commit_zstd(FILE* f, nflog_entry_t* store, uint32_t store_size);
+static void nfl_commit_default(FILE *f, nflog_entry_t *store,
+ uint32_t store_size);
+static void nfl_commit_lz4(FILE *f, nflog_entry_t *store, uint32_t store_size);
+static void nfl_commit_zstd(FILE *f, nflog_entry_t *store, uint32_t store_size);
-typedef void (*nflog_commit_run_table_t)(FILE* f, nflog_entry_t* store, uint32_t size);
+typedef void (*nflog_commit_run_table_t)(FILE *f, nflog_entry_t *store,
+ uint32_t size);
static const nflog_commit_run_table_t commit_run_table[] = {
- nfl_commit_default,
- nfl_commit_lz4,
- nfl_commit_zstd
-};
+ nfl_commit_default, nfl_commit_lz4, nfl_commit_zstd};
-void nfl_commit_init() {
- /* TODO */
-}
+void nfl_commit_init() { /* TODO */ }
-static void nfl_commit_default(FILE* f, nflog_entry_t* store, uint32_t store_size) {
+static void nfl_commit_default(FILE *f, nflog_entry_t *store,
+ uint32_t store_size) {
uint32_t written;
written = fwrite(store, 1, store_size, f);
ERR(written != store_size, strerror(errno));
}
-static void nfl_commit_lz4(FILE* f, nflog_entry_t* store, uint32_t store_size) {
+static void nfl_commit_lz4(FILE *f, nflog_entry_t *store, uint32_t store_size) {
/* TODO */
}
-static void nfl_commit_zstd(FILE* f, nflog_entry_t* store, uint32_t store_size) {
+static void nfl_commit_zstd(FILE *f, nflog_entry_t *store,
+ uint32_t store_size) {
size_t const bufsize = ZSTD_compressBound(store_size);
- void* buf;
+ void *buf;
ERR((buf = malloc(bufsize)), NULL);
@@ -44,8 +43,9 @@ static void nfl_commit_zstd(FILE* f, nflog_entry_t* store, uint32_t store_size)
free(buf);
}
-void nfl_commit_worker(nflog_header_t* header, nflog_entry_t* store, const char* filename) {
- FILE* f;
+void nfl_commit_worker(nflog_header_t *header, nflog_entry_t *store,
+ const char *filename) {
+ FILE *f;
uint32_t written;
debug("Comm worker #%u: commit to file %s\n", header->id, filename);
diff --git a/lib/common.c b/lib/common.c
index e544556..1f44e78 100644
--- a/lib/common.c
+++ b/lib/common.c
@@ -8,21 +8,21 @@
#include <unistd.h>
int nfl_check_file(FILE *f) {
- struct stat s;
- assert(f);
- if (fstat(fileno(f), &s) < 0)
- return -errno;
+ struct stat s;
+ assert(f);
+ if (fstat(fileno(f), &s) < 0)
+ return -errno;
- // Ignore file already unlinked
- if (s.st_nlink <= 0)
- return -EIDRM;
+ // Ignore file already unlinked
+ if (s.st_nlink <= 0)
+ return -EIDRM;
- return 0;
+ return 0;
}
int nfl_check_dir(const char *storage_dir) {
struct stat _d;
- if(stat(storage_dir, &_d) != 0 || !S_ISDIR(_d.st_mode)){
+ if (stat(storage_dir, &_d) != 0 || !S_ISDIR(_d.st_mode)) {
return -1;
}
return 0;
@@ -34,9 +34,9 @@ int nfl_storage_match_index(const char *fn) {
regmatch_t match[1];
int ret;
- if(!compiled) {
+ if (!compiled) {
ERR(regcomp(&regex, "^" STORAGE_PREFIX "_[0-9]+", 0),
- "Could not compile regex");
+ "Could not compile regex");
compiled = true;
}
@@ -44,8 +44,7 @@ int nfl_storage_match_index(const char *fn) {
if (!ret) {
assert(match[0].rm_so != (size_t)-1);
return strtol(fn + match[0].rm_so, NULL, 10);
- }
- else if (ret != REG_NOMATCH) {
+ } else if (ret != REG_NOMATCH) {
char buf[100];
regerror(ret, &regex, buf, sizeof(buf));
WARN(1, "Regex match failed: %s", buf)
@@ -60,61 +59,61 @@ const char *nfl_get_filename(const char *dir, int id) {
}
uint32_t nfl_header_cksum(nflog_header_t *header) {
- register uint64_t s = 3784672181;
- s += header->id;
- s ^= header->max_n_entries;
- s += header->n_entries;
- s ^= header->start_time;
- s += header->end_time;
- s &= ULONG_MAX;
- return s;
+ register uint64_t s = 3784672181;
+ s += header->id;
+ s ^= header->max_n_entries;
+ s += header->n_entries;
+ s ^= header->start_time;
+ s += header->end_time;
+ s &= ULONG_MAX;
+ return s;
}
-void nfl_cal_trunk(uint32_t total_size, uint32_t *trunk_cnt, uint32_t *trunk_size) {
- uint32_t pgsize = getpagesize();
+void nfl_cal_trunk(uint32_t total_size, uint32_t *trunk_cnt,
+ uint32_t *trunk_size) {
+ uint32_t pgsize = getpagesize();
total_size *= 1024 * 1024; // MiB
- assert(trunk_cnt);
- assert(total_size);
+ assert(trunk_cnt);
+ assert(total_size);
- *trunk_cnt = CEIL_DIV(total_size, pgsize*TRUNK_SIZE_BY_PAGE);
- if(*trunk_cnt > MAX_TRUNK_ID) {
+ *trunk_cnt = CEIL_DIV(total_size, pgsize * TRUNK_SIZE_BY_PAGE);
+ if (*trunk_cnt > MAX_TRUNK_ID) {
*trunk_cnt = MAX_TRUNK_ID;
- *trunk_size = total_size / MAX_TRUNK_ID;
- *trunk_size = (*trunk_size / pgsize) * pgsize; // align with pagesize
- }
- else {
- *trunk_size = pgsize*TRUNK_SIZE_BY_PAGE;
- }
+ *trunk_size = total_size / MAX_TRUNK_ID;
+ *trunk_size = (*trunk_size / pgsize) * pgsize; // align with pagesize
+ } else {
+ *trunk_size = pgsize * TRUNK_SIZE_BY_PAGE;
+ }
}
void nfl_cal_entries(uint32_t trunk_size, uint32_t *entries_cnt) {
- assert(entries_cnt);
- *entries_cnt = (trunk_size - sizeof(nflog_header_t)) / sizeof(nflog_entry_t);
+ assert(entries_cnt);
+ *entries_cnt =
+ (trunk_size - sizeof(nflog_header_t)) / sizeof(nflog_entry_t);
}
-void nfl_format_output(char* output, nflog_entry_t *entry) {
+void nfl_format_output(char *output, nflog_entry_t *entry) {
char dest_ip[16];
snprintf(dest_ip, 16, "%pI4", &entry->daddr);
- sprintf(output,
- "t=%ld\t"
- "daddr=%s\t"
- "proto=%s\t"
- "uid=%d\t"
- "sport=%d\t"
- "dport=%d",
- entry->timestamp, dest_ip,
- entry->protocol == IPPROTO_TCP ? "TCP" : "UDP",
- entry->uid, entry->sport, entry->dport);
+ sprintf(output, "t=%ld\t"
+ "daddr=%s\t"
+ "proto=%s\t"
+ "uid=%d\t"
+ "sport=%d\t"
+ "dport=%d",
+ entry->timestamp, dest_ip,
+ entry->protocol == IPPROTO_TCP ? "TCP" : "UDP", entry->uid,
+ entry->sport, entry->dport);
}
int nfl_setup_compression(const char *flag, enum nflog_compression_t *opt) {
- if(flag == NULL) {
- *opt=COMPRESS_NONE;
- } else if(!strcmp(flag, "zstd") || !strcmp(flag, "zstandard")) {
- *opt=COMPRESS_ZSTD;
- } else if(!strcmp(flag, "lz4")) {
- *opt=COMPRESS_LZ4;
+ if (flag == NULL) {
+ *opt = COMPRESS_NONE;
+ } else if (!strcmp(flag, "zstd") || !strcmp(flag, "zstandard")) {
+ *opt = COMPRESS_ZSTD;
+ } else if (!strcmp(flag, "lz4")) {
+ *opt = COMPRESS_LZ4;
} else {
fprintf(stderr, "Unknown compression algorithm: %s\n", flag);
return 0;
diff --git a/lib/extract.c b/lib/extract.c
index b33d183..4248089 100644
--- a/lib/extract.c
+++ b/lib/extract.c
@@ -8,24 +8,20 @@ static int nfl_extract_default(FILE *f, nflog_state_t *state);
static int nfl_extract_zstd(FILE *f, nflog_state_t *state);
static int nfl_extract_lz4(FILE *f, nflog_state_t *state);
-typedef int (*nflog_extract_run_table_t)(FILE* f, nflog_state_t* state);
+typedef int (*nflog_extract_run_table_t)(FILE *f, nflog_state_t *state);
static const nflog_extract_run_table_t extract_run_table[] = {
- nfl_extract_default,
- nfl_extract_lz4,
- nfl_extract_zstd
-};
+ nfl_extract_default, nfl_extract_lz4, nfl_extract_zstd};
static int nfl_verify_header(nflog_header_t *header) {
- if(header->id > MAX_TRUNK_ID)
+ if (header->id > MAX_TRUNK_ID)
return -1;
- if(header->max_n_entries < header->n_entries)
+ if (header->max_n_entries < header->n_entries)
return -1;
time_t now = time(NULL);
- if((time_t) header->start_time >= now ||
- (time_t) header->end_time >= now ||
- header->start_time > header->end_time)
+ if ((time_t)header->start_time >= now || (time_t)header->end_time >= now ||
+ header->start_time > header->end_time)
return -1;
return 0;
}
@@ -47,9 +43,9 @@ static int nfl_extract_lz4(FILE *f, nflog_state_t *state) {
}
int nfl_extract_worker(const char *filename, nflog_state_t *state) {
- FILE* f;
+ FILE *f;
int got = 0, ret = 0;
- nflog_header_t **header = &state->header;
+ nflog_header_t **header = &state->header;
nflog_entry_t **store = &state->store;
debug("Extracting from file %s", filename);
@@ -63,11 +59,12 @@ int nfl_extract_worker(const char *filename, nflog_state_t *state) {
// Check header validity
WARN_RETURN(ferror(f), "%s", strerror(errno));
WARN_RETURN(got != sizeof(nflog_header_t) || nfl_verify_header(*header) < 0,
- "File %s has corrupted header.", filename);
+ "File %s has corrupted header.", filename);
// Read body
- WARN_RETURN((*header)->compression_opt > sizeof(extract_run_table)/sizeof(extract_run_table[0]),
- "Unknown compression in %s", filename);
+ WARN_RETURN((*header)->compression_opt >
+ sizeof(extract_run_table) / sizeof(extract_run_table[0]),
+ "Unknown compression in %s", filename);
ERR((*store = malloc(sizeof(nflog_entry_t) * (*header)->n_entries)), NULL);
ret = extract_run_table[(*header)->compression_opt](f, state);
fclose(f);