aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYunchih Chen <yunchih.cat@gmail.com>2018-03-18 12:36:06 +0800
committerYunchih Chen <yunchih.cat@gmail.com>2018-03-18 12:36:06 +0800
commit20cf5af5efee4685271b083de23b6e66ba48d3f1 (patch)
tree48e3fc76d277cefbc8b3cf4b833e8f001d4e8917
parent9bb96d09226ecaafa45f2d153c6a9f176d593bb3 (diff)
downloadnfcollect-20cf5af5efee4685271b083de23b6e66ba48d3f1.tar
nfcollect-20cf5af5efee4685271b083de23b6e66ba48d3f1.tar.gz
nfcollect-20cf5af5efee4685271b083de23b6e66ba48d3f1.tar.bz2
nfcollect-20cf5af5efee4685271b083de23b6e66ba48d3f1.tar.lz
nfcollect-20cf5af5efee4685271b083de23b6e66ba48d3f1.tar.xz
nfcollect-20cf5af5efee4685271b083de23b6e66ba48d3f1.tar.zst
nfcollect-20cf5af5efee4685271b083de23b6e66ba48d3f1.zip
Add header checksum
-rw-r--r--include/common.h1
-rw-r--r--lib/collect.c3
-rw-r--r--lib/common.c14
-rw-r--r--lib/extract.c4
4 files changed, 15 insertions, 7 deletions
diff --git a/include/common.h b/include/common.h
index cfb454e..9b54143 100644
--- a/include/common.h
+++ b/include/common.h
@@ -6,6 +6,7 @@ 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);
uint32_t nfl_get_filesize(FILE *f);
+uint32_t nfl_header_cksum(nflog_header_t *header);
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);
diff --git a/lib/collect.c b/lib/collect.c
index b7cf430..a8a46bd 100644
--- a/lib/collect.c
+++ b/lib/collect.c
@@ -160,6 +160,9 @@ void *nfl_collect_worker(void *targs) {
nflog_unbind_group(nf->nfl_group_fd);
nflog_close(nf->nfl_fd);
+ // write checksum
+ nf->header->cksum = nfl_header_cksum(nf->header);
+
// spawn commit thread
nfl_commit(nf);
pthread_exit(NULL);
diff --git a/lib/common.c b/lib/common.c
index 804b803..344f531 100644
--- a/lib/common.c
+++ b/lib/common.c
@@ -72,14 +72,14 @@ uint32_t nfl_get_filesize(FILE *f) {
}
uint32_t nfl_header_cksum(nflog_header_t *header) {
+ #define H(s) (0x9e3779b9 + (s<< 6) + (s>> 1))
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 &= UINT_MAX;
- return s;
+ s ^= H(header->id);
+ s ^= H(header->max_n_entries);
+ s ^= H(header->n_entries);
+ s ^= H(header->start_time);
+ s ^= H(header->end_time);
+ return s & UINT_MAX;
}
void nfl_cal_trunk(uint32_t total_size, uint32_t *trunk_cnt,
diff --git a/lib/extract.c b/lib/extract.c
index dfb7223..be8dbc4 100644
--- a/lib/extract.c
+++ b/lib/extract.c
@@ -11,6 +11,9 @@ static int nfl_extract_zstd(FILE *f, nflog_state_t *state);
static int nfl_extract_lz4(FILE *f, nflog_state_t *state);
static int nfl_verify_header(nflog_header_t *header) {
+ if(header->cksum != nfl_header_cksum(header))
+ return -1;
+
if (header->id > MAX_TRUNK_ID)
return -1;
@@ -21,6 +24,7 @@ static int nfl_verify_header(nflog_header_t *header) {
if ((time_t)header->start_time >= now || (time_t)header->end_time >= now ||
header->start_time > header->end_time)
return -1;
+
return 0;
}