Go to the documentation of this file.
49 #if defined(UNITTESTS) || defined(FUZZ)
56 printf(
"FlowAlloc failed\n");
60 memset(
f, 0x00,
sizeof(
Flow));
64 if (family == AF_INET) {
66 }
else if (family == AF_INET6) {
71 if (family == AF_INET) {
72 if (inet_pton(AF_INET,
src, &in) != 1) {
73 printf(
"invalid address %s\n",
src);
77 f->
src.addr_data32[0] = in.s_addr;
83 if (family == AF_INET) {
84 if (inet_pton(AF_INET,
dst, &in) != 1) {
85 printf(
"invalid address %s\n",
dst);
89 f->
dst.addr_data32[0] = in.s_addr;
103 if (remove(
name) != 0) {
104 if (errno != ENOENT) {
105 printf(
"failed remove, errno=%d\n", errno);
109 FILE *fd = fopen(
name,
"wb");
111 printf(
"failed open, errno=%d\n", errno);
114 if (fwrite (data, 1, size, fd) != size) {
137 uint8_t ipproto,
const char *
src,
const char *
dst,
138 uint16_t sport, uint16_t dport)
154 if (inet_pton(AF_INET,
src, &in) != 1)
156 p->
src.addr_data32[0] = in.s_addr;
157 if (ipproto == IPPROTO_TCP || ipproto == IPPROTO_UDP || ipproto ==
IPPROTO_SCTP)
160 if (inet_pton(AF_INET,
dst, &in) != 1)
162 p->
dst.addr_data32[0] = in.s_addr;
163 if (ipproto == IPPROTO_TCP || ipproto == IPPROTO_UDP || ipproto ==
IPPROTO_SCTP)
170 ip4h->s_ip_src.s_addr =
p->
src.addr_data32[0];
171 ip4h->s_ip_dst.s_addr =
p->
dst.addr_data32[0];
176 int hdr_offset =
sizeof(
IPV4Hdr);
187 hdr_offset +=
sizeof(
UDPHdr);
201 hdr_offset +=
sizeof(
TCPHdr);
245 "192.168.1.5",
"192.168.1.1",
253 PacketSetIPV4(
p, (uint8_t *)ip4h);
258 PacketSetIPV6(
p, (uint8_t *)ip6h);
263 PacketSetTCP(
p, (uint8_t *)tcph);
276 if (inet_pton(AF_INET,
str, &in) != 1) {
277 printf(
"invalid IPv6 address %s\n",
str);
280 return (uint32_t)in.s_addr;
298 const char *
src,
const char *
dst, uint16_t sport, uint16_t dport)
318 ip6h->s_ip6_nxt = ipproto;
322 if (inet_pton(AF_INET6,
src, &in) != 1)
324 p->
src.addr_data32[0] = in[0];
325 p->
src.addr_data32[1] = in[1];
326 p->
src.addr_data32[2] = in[2];
327 p->
src.addr_data32[3] = in[3];
329 ip6h->s_ip6_src[0] = in[0];
330 ip6h->s_ip6_src[1] = in[1];
331 ip6h->s_ip6_src[2] = in[2];
332 ip6h->s_ip6_src[3] = in[3];
334 if (inet_pton(AF_INET6,
dst, &in) != 1)
336 p->
dst.addr_data32[0] = in[0];
337 p->
dst.addr_data32[1] = in[1];
338 p->
dst.addr_data32[2] = in[2];
339 p->
dst.addr_data32[3] = in[3];
341 ip6h->s_ip6_dst[0] = in[0];
342 ip6h->s_ip6_dst[1] = in[1];
343 ip6h->s_ip6_dst[2] = in[2];
344 ip6h->s_ip6_dst[3] = in[3];
349 memset(tcph, 0,
sizeof(
TCPHdr));
404 uint8_t ipproto,
const char *
src,
const char *
dst)
422 uint8_t ipproto,
const char *
src,
const char *
dst)
440 uint8_t ipproto, uint16_t sport, uint16_t dport)
443 "192.168.1.5",
"192.168.1.1",
459 for (; i < numpkts; i++) {
508 uint8_t *data, uint32_t data_len)
515 StreamingBufferSegment seg;
578 int UTHGenericTest(
Packet **pkt,
int numpkts,
const char *sigs[], uint32_t sids[], uint32_t *results,
int numsigs)
582 if (pkt == NULL || sigs == NULL || numpkts == 0
583 || sids == NULL || results == NULL || numsigs == 0) {
584 SCLogError(
"Arguments invalid, that the pointer/arrays are not NULL, and the number of "
585 "signatures and packets is > 0");
620 if (
p == NULL || sids == NULL) {
622 "packet is NULL, and if the array contain sids is set");
628 for (; i < numsigs; i++) {
630 if (r != results[i]) {
631 SCLogInfo(
"Sid %" PRIu32
" matched %" PRIu32
" times, and not %" PRIu32
" as expected",
632 sids[i], r, results[i]);
635 SCLogInfo(
"Sid %" PRIu32
" matched %" PRIu32
" times, as expected", sids[i], r);
657 for (
int i = 0; i < numsigs; i++) {
658 if (sigs[i] == NULL) {
702 for (
int i = 0; i < num_packets; i++) {
746 for (
int i = 0; i < num_packets; i++)
784 #ifdef BUILD_HYPERSCAN
785 }
else if (mpm_type ==
MPM_HS) {
789 BUG_ON(
"unsupported MPM type");
794 printf(
"de_ctx == NULL: ");
801 printf(
"signature == NULL: ");
810 printf(
"signature didn't alert: ");
880 memset(&fls, 0,
sizeof(fls));
882 memset(&
tv, 0,
sizeof(
tv));
885 uint8_t payload[] =
"Payload";
886 for (; i < end; i++) {
889 p->
src.addr_data32[0] = i;
890 p->
dst.addr_data32[0] = i + 1;
892 p->
src.addr_data32[0] = i + 1;
893 p->
dst.addr_data32[0] = i;
896 if (
p->
flow != NULL) {
939 static int CheckUTHTestPacket(
Packet *
p, uint8_t ipproto)
941 uint16_t sport = 41424;
943 uint8_t payload[] =
"Payload";
945 uint8_t
len =
sizeof(payload);
953 if (strncmp((
char *)payload, (
char *)
p->
payload,
len) != 0)
965 const UDPHdr *udph = PacketGetUDP(
p);
975 const TCPHdr *tcph = PacketGetTCP(
p);
990 void *
UTHmemsearch(
const void *big,
size_t big_len,
const void *little,
size_t little_len) {
991 return memmem(big, big_len, little, little_len);
995 void *
UTHmemsearch(
const void *big,
size_t big_len,
const void *little,
size_t little_len) {
996 return BasicSearch(big, big_len, little, little_len);
1003 static int UTHBuildPacketRealTest01(
void)
1005 uint8_t payload[] =
"Payload";
1008 "192.168.1.5",
"192.168.1.1", 41424, 80);
1010 int ret = CheckUTHTestPacket(
p, IPPROTO_TCP);
1019 static int UTHBuildPacketRealTest02(
void)
1021 uint8_t payload[] =
"Payload";
1024 "192.168.1.5",
"192.168.1.1", 41424, 80);
1026 int ret = CheckUTHTestPacket(
p, IPPROTO_UDP);
1034 static int UTHBuildPacketTest01(
void)
1036 uint8_t payload[] =
"Payload";
1040 int ret = CheckUTHTestPacket(
p, IPPROTO_TCP);
1049 static int UTHBuildPacketTest02(
void)
1051 uint8_t payload[] =
"Payload";
1055 int ret = CheckUTHTestPacket(
p, IPPROTO_UDP);
1064 static int UTHBuildPacketOfFlowsTest01(
void)
1086 static int UTHBuildPacketSrcDstTest01(
void)
1088 uint8_t payload[] =
"Payload";
1091 "192.168.1.5",
"192.168.1.1");
1093 int ret = CheckUTHTestPacket(
p, IPPROTO_TCP);
1102 static int UTHBuildPacketSrcDstTest02(
void)
1104 uint8_t payload[] =
"Payload";
1107 "192.168.1.5",
"192.168.1.1");
1109 int ret = CheckUTHTestPacket(
p, IPPROTO_UDP);
1118 static int UTHBuildPacketSrcDstPortsTest01(
void)
1120 uint8_t payload[] =
"Payload";
1125 int ret = CheckUTHTestPacket(
p, IPPROTO_TCP);
1134 static int UTHBuildPacketSrcDstPortsTest02(
void)
1136 uint8_t payload[] =
"Payload";
1141 int ret = CheckUTHTestPacket(
p, IPPROTO_UDP);
1152 UtRegisterTest(
"UTHBuildPacketRealTest01", UTHBuildPacketRealTest01);
1153 UtRegisterTest(
"UTHBuildPacketRealTest02", UTHBuildPacketRealTest02);
1156 UtRegisterTest(
"UTHBuildPacketSrcDstTest01", UTHBuildPacketSrcDstTest01);
1157 UtRegisterTest(
"UTHBuildPacketSrcDstTest02", UTHBuildPacketSrcDstTest02);
1159 UTHBuildPacketSrcDstPortsTest01);
1161 UTHBuildPacketSrcDstPortsTest02);
1162 UtRegisterTest(
"UTHBuildPacketOfFlowsTest01", UTHBuildPacketOfFlowsTest01);
FlowQueuePrivate work_queue
int UTHParseSignature(const char *str, bool expect)
parser a sig and see if the expected result is correct
void * UTHmemsearch(const void *big, size_t big_len, const void *little, size_t little_len)
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
int UTHAddStreamToFlow(Flow *f, int direction, uint8_t *data, uint32_t data_len)
uint32_t FlowSpareGetPoolSize(void)
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
Packet * UTHBuildPacketSrcDst(uint8_t *payload, uint16_t payload_len, uint8_t ipproto, const char *src, const char *dst)
UTHBuildPacketSrcDst is a wrapper that build packets specifying IPs and defaulting ports.
void UTHSetIPV4Hdr(Packet *p, IPV4Hdr *ip4h)
uint32_t UTHSetIPv4Address(const char *str)
return the uint32_t for a ipv4 address string
main detection engine ctx
int UTHPacketMatchSigMpm(Packet *p, char *sig, uint16_t mpm_type)
StreamingBufferConfig sbcnf
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
int PacketCopyDataOffset(Packet *p, uint32_t offset, const uint8_t *data, uint32_t datalen)
Copy data to Packet payload at given offset.
struct ICMPV4Hdr_ ICMPV4Hdr
void SCSigSignatureOrderingModuleCleanup(DetectEngineCtx *de_ctx)
De-registers all the signature ordering functions registered.
union PacketL3::Hdrs hdrs
int UTHCheckPacketMatchResults(Packet *p, uint32_t sids[], uint32_t results[], int numsigs)
UTHCheckPacketMatches: function to check if a packet match some sids.
int UTHPacketMatchSig(Packet *p, const char *sig)
int StreamingBufferAppend(StreamingBuffer *sb, const StreamingBufferConfig *cfg, StreamingBufferSegment *seg, const uint8_t *data, uint32_t data_len)
void FlowHandlePacket(ThreadVars *tv, FlowLookupStruct *fls, Packet *p)
Entry point for packet flow handling.
void UTHSetTCPHdr(Packet *p, TCPHdr *tcph)
Packet * UTHBuildPacket(uint8_t *payload, uint16_t payload_len, uint8_t ipproto)
UTHBuildPacket is a wrapper that build packets with default ip and port fields.
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
int SCSigOrderSignatures(DetectEngineCtx *de_ctx)
Orders the signatures.
Packet * UTHBuildPacketSrcDstPorts(uint8_t *payload, uint16_t payload_len, uint8_t ipproto, uint16_t sport, uint16_t dport)
UTHBuildPacketSrcDstPorts is a wrapper that build packets specifying src and dst ports and defaulting...
void UTHSetIPV6Hdr(Packet *p, IPV6Hdr *ip6h)
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
TcpStreamCnf stream_config
Packet * UTHBuildPacketReal(uint8_t *payload, uint16_t payload_len, uint8_t ipproto, const char *src, const char *dst, uint16_t sport, uint16_t dport)
UTHBuildPacketReal is a function that create tcp/udp packets for unittests specifying ip and port sou...
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
#define FLOWLOCK_UNLOCK(fb)
uint32_t UTHBuildPacketOfFlows(uint32_t start, uint32_t end, uint8_t dir)
void UTHAssignFlow(Packet *p, Flow *f)
void FlowInitConfig(bool quiet)
initialize the configuration
#define SET_PKT_LEN(p, len)
int UTHMatchPackets(DetectEngineCtx *de_ctx, Packet **p, int num_packets)
Flow * UTHBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp)
#define FLOW_INITIALIZE(f)
Packet * UTHBuildPacketIPV6Real(uint8_t *payload, uint16_t payload_len, uint8_t ipproto, const char *src, const char *dst, uint16_t sport, uint16_t dport)
UTHBuildPacketReal is a function that create tcp/udp packets for unittests specifying ip and port sou...
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
#define PASS
Pass the test.
int UTHMatchPacketsWithResults(DetectEngineCtx *de_ctx, Packet **p, int num_packets, uint32_t sids[], uint32_t *results, int numsigs)
void UTHRegisterTests(void)
int UTHAddSessionToFlow(Flow *f, uint32_t ts_isn, uint32_t tc_isn)
Per thread variable structure.
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
void SCSigRegisterSignatureOrderingFuncs(DetectEngineCtx *de_ctx)
Lets you register the Signature ordering functions. The order in which the functions are registered s...
void PacketFree(Packet *p)
Return a malloced packet.
int TestHelperBufferToFile(const char *name, const uint8_t *data, size_t size)
writes the contents of a buffer into a file
Flow * FlowQueuePrivateGetFromTop(FlowQueuePrivate *fqc)
uint8_t * BasicSearch(const uint8_t *haystack, uint32_t haystack_len, const uint8_t *needle, uint16_t needle_len)
Basic search improved. Limits are better handled, so it doesn't start searches that wont fit in the r...
#define STREAMING_BUFFER_INITIALIZER
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
void StatsThreadInit(StatsThreadContext *stats)
void UTHFreeFlow(Flow *flow)
FlowQueuePrivate spare_queue
void FlowFree(Flow *f)
cleanup & free the memory of a flow
Packet * UTHBuildPacketIPV6SrcDst(uint8_t *payload, uint16_t payload_len, uint8_t ipproto, const char *src, const char *dst)
UTHBuildPacketSrcDst is a wrapper that build packets specifying IPs and defaulting ports (IPV6)
void FlowShutdown(void)
shutdown the flow engine
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
uint8_t app_update_direction
int UTHGenericTest(Packet **pkt, int numpkts, const char *sigs[], uint32_t sids[], uint32_t *results, int numsigs)
UTHGenericTest: function that perform a generic check taking care of as maximum common unittest eleme...
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
#define SCLogError(...)
Macro used to log ERROR messages.
Structure to hold thread specific data for all decode modules.
void UTHFreePacket(Packet *p)
UTHFreePacket: function to release the allocated data from UTHBuildPacket and the packet itself.
void StreamTcpSessionCleanup(TcpSession *ssn)
Session cleanup function. Does not free the ssn.
union PacketL4::L4Hdrs hdrs
Packet * UTHBuildPacketFromEth(uint8_t *raw_eth, uint16_t pktsize)
UTHBuildPacketFromEth is a wrapper that build a packet for the rawbytes.
int SCConfSet(const char *name, const char *val)
Set a configuration value.
DetectEngineCtx * DetectEngineCtxInit(void)
Flow * TestHelperBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp)
int UTHAppendSigs(DetectEngineCtx *de_ctx, const char *sigs[], int numsigs)
UTHAppendSigs: Add sigs to the detection_engine checking for errors.
int UTHRemoveSessionFromFlow(Flow *f)
void StatsThreadCleanup(StatsThreadContext *stats)
int DecodeEthernet(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
void UTHFreePackets(Packet **p, int numpkts)
UTHFreePackets: function to release the allocated data from UTHBuildPacket and the packet itself.