aboutsummaryrefslogtreecommitdiffstats
path: root/lib
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 /lib
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
Diffstat (limited to 'lib')
-rw-r--r--lib/collect.c55
-rw-r--r--lib/commit.c36
-rw-r--r--lib/common.c103
-rw-r--r--lib/extract.c27
4 files changed, 111 insertions, 110 deletions
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);