51#if defined(UNITTESTS) || defined(FUZZ)
58 printf(
"FlowAlloc failed\n");
62 memset(f, 0x00,
sizeof(
Flow));
66 if (family == AF_INET) {
68 }
else if (family == AF_INET6) {
73 if (family == AF_INET) {
74 if (inet_pton(AF_INET,
src, &in) != 1) {
75 printf(
"invalid address %s\n",
src);
79 f->src.addr_data32[0] = in.s_addr;
85 if (family == AF_INET) {
86 if (inet_pton(AF_INET,
dst, &in) != 1) {
87 printf(
"invalid address %s\n",
dst);
91 f->dst.addr_data32[0] = in.s_addr;
105 if (remove(
name) != 0) {
106 if (errno != ENOENT) {
107 printf(
"failed remove, errno=%d\n", errno);
111 FILE *fd = fopen(
name,
"wb");
113 printf(
"failed open, errno=%d\n", errno);
116 if (fwrite (data, 1, size, fd) != size) {
128 PacketSetIPV4(p, (uint8_t *)ip4h);
133 PacketSetIPV6(p, (uint8_t *)ip6h);
138 PacketSetTCP(p, (uint8_t *)tcph);
151 if (inet_pton(AF_INET,
str, &in) != 1) {
152 printf(
"invalid IPv6 address %s\n",
str);
155 return (uint32_t)in.s_addr;
173 uint8_t ipproto,
const char *
src,
const char *
dst,
174 uint16_t sport, uint16_t dport)
194 ip6h->s_ip6_nxt = ipproto;
198 if (inet_pton(AF_INET6,
src, &in) != 1)
200 p->
src.addr_data32[0] = in[0];
201 p->
src.addr_data32[1] = in[1];
202 p->
src.addr_data32[2] = in[2];
203 p->
src.addr_data32[3] = in[3];
205 ip6h->s_ip6_src[0] = in[0];
206 ip6h->s_ip6_src[1] = in[1];
207 ip6h->s_ip6_src[2] = in[2];
208 ip6h->s_ip6_src[3] = in[3];
210 if (inet_pton(AF_INET6,
dst, &in) != 1)
212 p->
dst.addr_data32[0] = in[0];
213 p->
dst.addr_data32[1] = in[1];
214 p->
dst.addr_data32[2] = in[2];
215 p->
dst.addr_data32[3] = in[3];
217 ip6h->s_ip6_dst[0] = in[0];
218 ip6h->s_ip6_dst[1] = in[1];
219 ip6h->s_ip6_dst[2] = in[2];
220 ip6h->s_ip6_dst[3] = in[3];
225 memset(tcph, 0,
sizeof(
TCPHdr));
261 uint8_t ipproto,
const char *
src,
const char *
dst,
262 uint16_t sport, uint16_t dport)
272 p->src.family = AF_INET;
273 p->dst.family = AF_INET;
274 p->payload = payload;
278 if (inet_pton(AF_INET,
src, &in) != 1)
280 p->src.addr_data32[0] = in.s_addr;
281 if (ipproto == IPPROTO_TCP || ipproto == IPPROTO_UDP || ipproto ==
IPPROTO_SCTP)
284 if (inet_pton(AF_INET,
dst, &in) != 1)
286 p->dst.addr_data32[0] = in.s_addr;
287 if (ipproto == IPPROTO_TCP || ipproto == IPPROTO_UDP || ipproto ==
IPPROTO_SCTP)
294 ip4h->s_ip_src.s_addr = p->src.addr_data32[0];
295 ip4h->s_ip_dst.s_addr = p->dst.addr_data32[0];
300 int hdr_offset =
sizeof(
IPV4Hdr);
311 hdr_offset +=
sizeof(
UDPHdr);
325 hdr_offset +=
sizeof(
TCPHdr);
369 "192.168.1.5",
"192.168.1.1",
390 memset(&th_v, 0,
sizeof(th_v));
407 uint8_t ipproto,
const char *
src,
const char *
dst)
425 uint8_t ipproto,
const char *
src,
const char *
dst)
443 uint8_t ipproto, uint16_t sport, uint16_t dport)
446 "192.168.1.5",
"192.168.1.1",
462 for (; i < numpkts; i++) {
501 uint8_t *data, uint32_t data_len)
508 StreamingBufferSegment seg;
571int UTHGenericTest(
Packet **pkt,
int numpkts,
const char *sigs[], uint32_t sids[], uint32_t *results,
int numsigs)
575 if (pkt == NULL || sigs == NULL || numpkts == 0
576 || sids == NULL || results == NULL || numsigs == 0) {
577 SCLogError(
"Arguments invalid, that the pointer/arrays are not NULL, and the number of "
578 "signatures and packets is > 0");
613 if (p == NULL || sids == NULL) {
615 "packet is NULL, and if the array contain sids is set");
621 for (; i < numsigs; i++) {
623 if (r != results[i]) {
624 SCLogInfo(
"Sid %" PRIu32
" matched %" PRIu32
" times, and not %" PRIu32
" as expected",
625 sids[i], r, results[i]);
628 SCLogInfo(
"Sid %" PRIu32
" matched %" PRIu32
" times, as expected", sids[i], r);
650 for (
int i = 0; i < numsigs; i++) {
651 if (sigs[i] == NULL) {
689 memset(&th_v, 0,
sizeof(th_v));
694 for (
int i = 0; i < num_packets; i++) {
727 memset(&th_v, 0,
sizeof(th_v));
734 for (
int i = 0; i < num_packets; i++)
767 memset(&th_v, 0,
sizeof(th_v));
771 printf(
"de_ctx == NULL: ");
780 printf(
"signature == NULL: ");
789 printf(
"signature didn't alert: ");
820 memset(&th_v, 0,
sizeof(th_v));
862 memset(&fls, 0,
sizeof(fls));
864 memset(&
tv, 0,
sizeof(
tv));
867 uint8_t payload[] =
"Payload";
868 for (; i < end; i++) {
871 p->
src.addr_data32[0] = i;
872 p->
dst.addr_data32[0] = i + 1;
874 p->
src.addr_data32[0] = i + 1;
875 p->
dst.addr_data32[0] = i;
878 if (p->
flow != NULL) {
921static int CheckUTHTestPacket(
Packet *p, uint8_t ipproto)
923 uint16_t sport = 41424;
925 uint8_t payload[] =
"Payload";
927 uint8_t
len =
sizeof(payload);
935 if (strncmp((
char *)payload, (
char *)p->
payload,
len) != 0)
942 if (p->
proto != ipproto)
947 const UDPHdr *udph = PacketGetUDP(p);
957 const TCPHdr *tcph = PacketGetTCP(p);
972void *
UTHmemsearch(
const void *big,
size_t big_len,
const void *little,
size_t little_len) {
973 return memmem(big, big_len, little, little_len);
977void *
UTHmemsearch(
const void *big,
size_t big_len,
const void *little,
size_t little_len) {
978 return BasicSearch(big, big_len, little, little_len);
985static int UTHBuildPacketRealTest01(
void)
987 uint8_t payload[] =
"Payload";
990 "192.168.1.5",
"192.168.1.1", 41424, 80);
992 int ret = CheckUTHTestPacket(p, IPPROTO_TCP);
1001static int UTHBuildPacketRealTest02(
void)
1003 uint8_t payload[] =
"Payload";
1006 "192.168.1.5",
"192.168.1.1", 41424, 80);
1008 int ret = CheckUTHTestPacket(p, IPPROTO_UDP);
1016static int UTHBuildPacketTest01(
void)
1018 uint8_t payload[] =
"Payload";
1022 int ret = CheckUTHTestPacket(p, IPPROTO_TCP);
1031static int UTHBuildPacketTest02(
void)
1033 uint8_t payload[] =
"Payload";
1037 int ret = CheckUTHTestPacket(p, IPPROTO_UDP);
1046static int UTHBuildPacketOfFlowsTest01(
void)
1068static int UTHBuildPacketSrcDstTest01(
void)
1070 uint8_t payload[] =
"Payload";
1073 "192.168.1.5",
"192.168.1.1");
1075 int ret = CheckUTHTestPacket(p, IPPROTO_TCP);
1084static int UTHBuildPacketSrcDstTest02(
void)
1086 uint8_t payload[] =
"Payload";
1089 "192.168.1.5",
"192.168.1.1");
1091 int ret = CheckUTHTestPacket(p, IPPROTO_UDP);
1100static int UTHBuildPacketSrcDstPortsTest01(
void)
1102 uint8_t payload[] =
"Payload";
1107 int ret = CheckUTHTestPacket(p, IPPROTO_TCP);
1116static int UTHBuildPacketSrcDstPortsTest02(
void)
1118 uint8_t payload[] =
"Payload";
1123 int ret = CheckUTHTestPacket(p, IPPROTO_UDP);
1134 UtRegisterTest(
"UTHBuildPacketRealTest01", UTHBuildPacketRealTest01);
1135 UtRegisterTest(
"UTHBuildPacketRealTest02", UTHBuildPacketRealTest02);
1138 UtRegisterTest(
"UTHBuildPacketSrcDstTest01", UTHBuildPacketSrcDstTest01);
1139 UtRegisterTest(
"UTHBuildPacketSrcDstTest02", UTHBuildPacketSrcDstTest02);
1141 UTHBuildPacketSrcDstPortsTest01);
1143 UTHBuildPacketSrcDstPortsTest02);
1144 UtRegisterTest(
"UTHBuildPacketOfFlowsTest01", UTHBuildPacketOfFlowsTest01);
int DecodeEthernet(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
struct ICMPV4Hdr_ ICMPV4Hdr
#define SET_PKT_LEN(p, len)
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
void SigCleanSignatures(DetectEngineCtx *de_ctx)
int SigGroupCleanup(DetectEngineCtx *de_ctx)
void SCSigOrderSignatures(DetectEngineCtx *de_ctx)
Orders the signatures.
DetectEngineCtx * DetectEngineCtxInit(void)
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
void SCSigSignatureOrderingModuleCleanup(DetectEngineCtx *de_ctx)
De-registers all the signature ordering functions registered.
void SCSigRegisterSignatureOrderingFuncs(DetectEngineCtx *de_ctx)
Lets you register the Signature ordering functions. The order in which the functions are registered s...
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
Signature * SigInit(DetectEngineCtx *de_ctx, const char *sigstr)
Parses a signature and adds it to the Detection Engine Context.
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
TcpStreamCnf stream_config
Flow * FlowQueuePrivateGetFromTop(FlowQueuePrivate *fqc)
uint32_t FlowSpareGetPoolSize(void)
void FlowFree(Flow *f)
cleanup & free the memory of a flow
#define FLOW_INITIALIZE(f)
void FlowHandlePacket(ThreadVars *tv, FlowLookupStruct *fls, Packet *p)
Entry point for packet flow handling.
void FlowInitConfig(bool quiet)
initialize the configuration
void FlowShutdown(void)
shutdown the flow engine
#define FLOWLOCK_UNLOCK(fb)
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
#define PASS
Pass the test.
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
int PacketCopyDataOffset(Packet *p, uint32_t offset, const uint8_t *data, uint32_t datalen)
Copy data to Packet payload at given offset.
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
void PacketFree(Packet *p)
Return a malloced packet.
void StreamTcpSessionCleanup(TcpSession *ssn)
Session cleanup function. Does not free the ssn.
Structure to hold thread specific data for all decode modules.
main detection engine ctx
FlowQueuePrivate spare_queue
FlowQueuePrivate work_queue
StreamingBufferConfig sbcnf
Per thread variable structure.
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
#define SCLogError(...)
Macro used to log ERROR messages.
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...
int StreamingBufferAppend(StreamingBuffer *sb, const StreamingBufferConfig *cfg, StreamingBufferSegment *seg, const uint8_t *data, uint32_t data_len)
#define STREAMING_BUFFER_INITIALIZER
int TestHelperBufferToFile(const char *name, const uint8_t *data, size_t size)
writes the contents of a buffer into a file
void UTHFreePackets(Packet **p, int numpkts)
UTHFreePackets: function to release the allocated data from UTHBuildPacket and the packet itself.
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...
int UTHPacketMatchSig(Packet *p, const char *sig)
int UTHRemoveSessionFromFlow(Flow *f)
void * UTHmemsearch(const void *big, size_t big_len, const void *little, size_t little_len)
int UTHMatchPackets(DetectEngineCtx *de_ctx, Packet **p, int num_packets)
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.
int UTHAddSessionToFlow(Flow *f, uint32_t ts_isn, uint32_t tc_isn)
void UTHSetIPV4Hdr(Packet *p, IPV4Hdr *ip4h)
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...
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.
int UTHAppendSigs(DetectEngineCtx *de_ctx, const char *sigs[], int numsigs)
UTHAppendSigs: Add sigs to the detection_engine checking for errors.
int UTHMatchPacketsWithResults(DetectEngineCtx *de_ctx, Packet **p, int num_packets, uint32_t sids[], uint32_t *results, int numsigs)
void UTHAssignFlow(Packet *p, Flow *f)
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...
int UTHCheckPacketMatchResults(Packet *p, uint32_t sids[], uint32_t results[], int numsigs)
UTHCheckPacketMatches: function to check if a packet match some sids.
void UTHFreeFlow(Flow *flow)
Flow * TestHelperBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp)
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...
void UTHRegisterTests(void)
Packet * UTHBuildPacketFromEth(uint8_t *raw_eth, uint16_t pktsize)
UTHBuildPacketFromEth is a wrapper that build a packet for the rawbytes.
void UTHFreePacket(Packet *p)
UTHFreePacket: function to release the allocated data from UTHBuildPacket and the packet itself.
int UTHAddStreamToFlow(Flow *f, int direction, uint8_t *data, uint32_t data_len)
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)
int UTHParseSignature(const char *str, bool expect)
parser a sig and see if the expected result is correct
int UTHPacketMatchSigMpm(Packet *p, char *sig, uint16_t mpm_type)
void UTHSetTCPHdr(Packet *p, TCPHdr *tcph)
void UTHSetIPV6Hdr(Packet *p, IPV6Hdr *ip6h)
uint32_t UTHBuildPacketOfFlows(uint32_t start, uint32_t end, uint8_t dir)
uint32_t UTHSetIPv4Address(const char *str)
return the uint32_t for a ipv4 address string
Flow * UTHBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp)