Add native function to obtain netfilter conntrack dump as XML

This commit is contained in:
Harald Welte 2017-07-04 19:38:41 +01:00
parent 7602503a3a
commit c20f6e99e5
4 changed files with 62 additions and 3 deletions

View File

@ -37,7 +37,7 @@ CPPFLAGS = -D$(PLATFORM) -I$(TTCN3_DIR)/include -I/usr/include/titan
CXXFLAGS = -Wall
# Flags for the linker:
LDFLAGS = -L /usr/lib/titan
LDFLAGS = -L /usr/lib/titan `pkg-config --libs libnetfilter_conntrack`
ifeq ($(PLATFORM), WIN32)
# Silence linker warnings.
@ -52,7 +52,7 @@ ARFLAGS =
COMPILER_FLAGS = -L
# Execution mode: (either ttcn3 or ttcn3-parallel)
TTCN3_LIB = ttcn3-parallel
TTCN3_LIB = ttcn3
# The path of your libxml2 installation:
# If you do not have your own one, leave it unchanged.

View File

@ -0,0 +1,53 @@
#include <string.h>
#include <libnetfilter_conntrack/libnetfilter_conntrack.h>
#include "NetfilterConntrack_Functions.hh"
namespace NetfilterConntrack__Functions
{
/* call-back function called for every matching conntrack entry */
static int cb(enum nf_conntrack_msg_type type, struct nf_conntrack *ct, void *data)
{
CHARSTRING *ret_val = (CHARSTRING *) data;
char buf[1024];
nfct_snprintf(buf, sizeof(buf), ct, NFCT_T_UNKNOWN, NFCT_O_XML, NFCT_OF_SHOW_LAYER3 | NFCT_OF_TIMESTAMP);
//printf(buf);
*ret_val += buf;
return NFCT_CB_CONTINUE;
}
/* external function exposed to TTCN3 */
CHARSTRING f__get__conntracks__xml(void)
{
#if 0
const char *flow = "<flow xmlns=\"http://www.netfilter.org/xml/libnetfilter_conntrack\"><meta direction=\"original\"><layer3 protonum=\"2\" protoname=\"ipv4\"><src>100.86.194.120</src><dst>195.238.226.19</dst></layer3><layer4 protonum=\"6\" protoname=\"tcp\"><sport>50528</sport><dport>443</dport></layer4></meta><meta direction=\"reply\"><layer3 protonum=\"2\" protoname=\"ipv4\"><src>195.238.226.19</src><dst>100.86.194.120</dst></layer3><layer4 protonum=\"6\" protoname=\"tcp\"><sport>443</sport><dport>50528</dport></layer4></meta><meta direction=\"independent\"><state>ESTABLISHED</state><timeout>423663</timeout><mark>0</mark><use>1</use><id>477116416</id><assured/></meta></flow>";
CHARSTRING ret_val(strlen(flow), flow);
return ret_val;
#else
struct nfct_handle *h;
uint32_t family = AF_INET;
int rc;
CHARSTRING ret_val("");
h = nfct_open(CONNTRACK, 0);
if (!h) {
perror("nfct_open");
return ret_val;
}
printf("ret_val=%p\n", &ret_val);
nfct_callback_register(h, NFCT_T_ALL, cb, (void *) &ret_val);
rc = nfct_query(h, NFCT_Q_DUMP, &family);
if (rc < 0)
return ret_val;
nfct_close(h);
return ret_val;
#endif
}
}

View File

@ -0,0 +1,5 @@
module NetfilterConntrack_Functions {
external function f_get_conntracks_xml() return charstring;
}

View File

@ -4,5 +4,6 @@ FILES="*.ttcn *.cc *.hh"
ttcn3_makefilegen -f $FILES
sed -i -e 's/# TTCN3_DIR = /TTCN3_DIR = \/usr/' Makefile
sed -i -e 's/LDFLAGS = /LDFLAGS = -L \/usr\/lib\/titan/' Makefile
sed -i -e 's/LDFLAGS = /LDFLAGS = -L \/usr\/lib\/titan `pkg-config --libs libnetfilter_conntrack`/' Makefile
sed -i -e 's/TTCN3_LIB = ttcn3-parallel/TTCN3_LIB = ttcn3/' Makefile
sed -i -e 's/CPPFLAGS = -D$(PLATFORM) -I$(TTCN3_DIR)\/include/CPPFLAGS = -D$(PLATFORM) -I$(TTCN3_DIR)\/include -I\/usr\/include\/titan/' Makefile