aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYunchih Chen <yunchih.cat@gmail.com>2017-12-06 00:13:16 +0800
committerYunchih Chen <yunchih.cat@gmail.com>2017-12-06 00:13:16 +0800
commitbfe4f8470c744766bda97ec19f71ac1ae1acebea (patch)
tree56be30bfc4fd42ac213f30c2fdf6373efbb5cf80
parent0503266b60eb42a3714f0c29afec0b9f282558a9 (diff)
downloadnfcollect-bfe4f8470c744766bda97ec19f71ac1ae1acebea.tar
nfcollect-bfe4f8470c744766bda97ec19f71ac1ae1acebea.tar.gz
nfcollect-bfe4f8470c744766bda97ec19f71ac1ae1acebea.tar.bz2
nfcollect-bfe4f8470c744766bda97ec19f71ac1ae1acebea.tar.lz
nfcollect-bfe4f8470c744766bda97ec19f71ac1ae1acebea.tar.xz
nfcollect-bfe4f8470c744766bda97ec19f71ac1ae1acebea.tar.zst
nfcollect-bfe4f8470c744766bda97ec19f71ac1ae1acebea.zip
Add entry counter inside the packet handler
-rw-r--r--main.h3
-rw-r--r--nflog.c8
2 files changed, 9 insertions, 2 deletions
diff --git a/main.h b/main.h
index c955b18..ff975fe 100644
--- a/main.h
+++ b/main.h
@@ -52,6 +52,9 @@
fprintf(stdout, format "\n", ##__VA_ARGS__); \
}
+#define likely(x) __builtin_expect((x),1)
+#define unlikely(x) __builtin_expect((x),0)
+
#define CEILING(a,b) ((a)%(b) == 0 ? ((a)/(b)) : ((a)/(b)+1))
#define TRUNK_SIZE (4096 * 150)
diff --git a/nflog.c b/nflog.c
index 051c904..ff9480b 100644
--- a/nflog.c
+++ b/nflog.c
@@ -53,7 +53,9 @@ static int handle_packet(struct nflog_g_handle *gh, struct nfgenmsg *nfmsg,
nflog_state_t *nf = (nflog_state_t *)_nf;
// only process ipv4 packet
- if (payload_len < 0 || ((payload[0] & 0xf0) != 0x40))
+ if (unlikely(payload_len < 0) || ((payload[0] & 0xf0) != 0x40))
+ return 1;
+ if (unlikely(nf->header->n_entries >= nf->header->max_n_entries))
return 1;
iph = (struct iphdr *)payload;
@@ -88,13 +90,14 @@ static int handle_packet(struct nflog_g_handle *gh, struct nfgenmsg *nfmsg,
time(&entry->timestamp);
nf->header->n_entries++;
- debug("Recv packet info: "
+ debug("Recv packet info entry #%d: "
"timestamp:\t%ld\t"
"daddr:\t%d\t"
"transfer:\t%s\t"
"uid:\t%d\t"
"sport:\t%d\t"
"dport:\t%d",
+ nf->header->n_entries,
entry->timestamp, entry->daddr,
iph->protocol == IPPROTO_TCP ? "TCP" : "UDP",
entry->uid, entry->sport, entry->dport);
@@ -147,6 +150,7 @@ void *nflog_worker(void *targs) {
}
}
+ debug("Recv worker #%u: finish recv", nf->header->id);
time(&nf->header->end_time);
nfl_cleanup(nf);
nfl_commit(nf);