• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OISF / suricata / 23374838686

21 Mar 2026 07:29AM UTC coverage: 59.341% (-20.0%) from 79.315%
23374838686

Pull #15075

github

web-flow
Merge 90b4e834f into 6587e363a
Pull Request #15075: Stack 8001 v16.4

38 of 70 new or added lines in 10 files covered. (54.29%)

34165 existing lines in 563 files now uncovered.

119621 of 201584 relevant lines covered (59.34%)

650666.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.22
/src/detect-sameip.c
1
/* Copyright (C) 2007-2021 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17

18
/**
19
 * \file
20
 *
21
 * \author Brian Rectanus <brectanu@gmail.com>
22
 *
23
 * Implements the sameip keyword.
24
 */
25

26
#include "suricata-common.h"
27
#include "decode.h"
28
#include "detect.h"
29

30
#include "detect-parse.h"
31
#include "detect-engine.h"
32
#include "detect-engine-mpm.h"
33
#include "detect-engine-build.h"
34

35
#include "detect-sameip.h"
36

37
#include "util-unittest.h"
38
#include "util-unittest-helper.h"
39

40
static int DetectSameipMatch(DetectEngineThreadCtx *, Packet *,
41
                             const Signature *, const SigMatchCtx *);
42
static int DetectSameipSetup(DetectEngineCtx *, Signature *, const char *);
43
#ifdef UNITTESTS
44
static void DetectSameipRegisterTests(void);
45
#endif
46

47
/**
48
 * \brief Registration function for sameip: keyword
49
 */
50
void DetectSameipRegister(void)
51
{
3✔
52
    sigmatch_table[DETECT_SAMEIP].name = "sameip";
3✔
53
    sigmatch_table[DETECT_SAMEIP].desc = "check if the IP address of the source is the same as the IP address of the destination";
3✔
54
    sigmatch_table[DETECT_SAMEIP].url = "/rules/header-keywords.html#sameip";
3✔
55
    sigmatch_table[DETECT_SAMEIP].Match = DetectSameipMatch;
3✔
56
    sigmatch_table[DETECT_SAMEIP].Setup = DetectSameipSetup;
3✔
57
#ifdef UNITTESTS
58
    sigmatch_table[DETECT_SAMEIP].RegisterTests = DetectSameipRegisterTests;
59
#endif
60
    sigmatch_table[DETECT_SAMEIP].flags = SIGMATCH_NOOPT;
3✔
61
}
3✔
62

63
/**
64
 * \internal
65
 * \brief This function is used to match packets with same src/dst IPs
66
 *
67
 * \param t pointer to thread vars
68
 * \param det_ctx pointer to the pattern matcher thread
69
 * \param p pointer to the current packet
70
 * \param m pointer to the sigmatch that we will cast into DetectSameipData
71
 *
72
 * \retval 0 no match
73
 * \retval 1 match
74
 */
75
static int DetectSameipMatch(DetectEngineThreadCtx *det_ctx,
76
                             Packet *p, const Signature *s, const SigMatchCtx *ctx)
UNCOV
77
{
×
UNCOV
78
    return CMP_ADDR(&p->src, &p->dst) ? 1 : 0;
×
UNCOV
79
}
×
80

81
/**
82
 * \internal
83
 * \brief this function is used to add the sameip option into the signature
84
 *
85
 * \param de_ctx pointer to the Detection Engine Context
86
 * \param s pointer to the Current Signature
87
 * \param optstr pointer to the user provided options
88
 *
89
 * \retval 0 on Success
90
 * \retval -1 on Failure
91
 */
92
static int DetectSameipSetup(DetectEngineCtx *de_ctx, Signature *s, const char *optstr)
93
{
76✔
94
    /* Get this into a SigMatch and put it in the Signature. */
95
    if (SCSigMatchAppendSMToList(de_ctx, s, DETECT_SAMEIP, NULL, DETECT_SM_LIST_MATCH) == NULL) {
76✔
96
        return -1;
×
97
    }
×
98
    s->flags |= SIG_FLAG_REQUIRE_PACKET;
76✔
99
    return 0;
76✔
100
}
76✔
101

102
#ifdef UNITTESTS
103
#include "detect-engine-alert.h"
104

105
/* NOTE: No parameters, so no parse tests */
106

107
/**
108
 * \internal
109
 * \brief This test tests sameip success and failure.
110
 */
111
static int DetectSameipSigTest01(void)
112
{
113
    uint8_t *buf = (uint8_t *)
114
                    "GET / HTTP/1.0\r\n"
115
                    "\r\n";
116
    uint16_t buflen = strlen((char *)buf);
117
    ThreadVars th_v;
118
    DetectEngineThreadCtx *det_ctx;
119

120
    memset(&th_v, 0, sizeof(th_v));
121
    StatsThreadInit(&th_v.stats);
122

123
    /* First packet has same IPs */
124
    Packet *p1 = UTHBuildPacketSrcDst(buf, buflen, IPPROTO_TCP, "1.2.3.4", "1.2.3.4");
125

126
    /* Second packet does not have same IPs */
127
    Packet *p2 = UTHBuildPacketSrcDst(buf, buflen, IPPROTO_TCP, "1.2.3.4", "4.3.2.1");
128

129
    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
130
    FAIL_IF_NULL(de_ctx);
131
    de_ctx->flags |= DE_QUIET;
132

133
    Signature *s = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
134
                                                 "(msg:\"Testing sameip\"; sameip; sid:1;)");
135
    FAIL_IF_NULL(s);
136

137
    SigGroupBuild(de_ctx);
138
    DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
139

140
    SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
141
    FAIL_IF(PacketAlertCheck(p1, 1) == 0);
142

143
    SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
144
    FAIL_IF(PacketAlertCheck(p2, 1) != 0);
145

146
    UTHFreePacket(p1);
147
    UTHFreePacket(p2);
148

149
    DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
150
    DetectEngineCtxFree(de_ctx);
151
    StatsThreadCleanup(&th_v.stats);
152
    PASS;
153
}
154

155
/**
156
 * \internal
157
 * \brief This function registers unit tests for DetectSameip
158
 */
159
static void DetectSameipRegisterTests(void)
160
{
161
    UtRegisterTest("DetectSameipSigTest01", DetectSameipSigTest01);
162
}
163
#endif /* UNITTESTS */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc