Skip to content

Commit b575ae3

Browse files
Lukas Sismisvictorjulien
authored andcommitted
pcap-file: move packet counter to PCAP packet structure
Code refactor to gather all PCAP-related structure members under one structure. New pcap_v structure guards protect the union variables from other capture modes trying to access the packet number incorrectly. Ticket: 7835
1 parent 400328c commit b575ae3

4 files changed

Lines changed: 28 additions & 16 deletions

File tree

src/decode.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,27 @@ void PacketAlertGetMaxConfig(void)
10951095
SCLogDebug("detect->packet_alert_max set to %d", packet_alert_max);
10961096
}
10971097

1098+
static inline bool PcapPacketCntRunmodeCanAccess(void)
1099+
{
1100+
SCRunMode m = SCRunmodeGet();
1101+
return m == RUNMODE_PCAP_FILE || m == RUNMODE_UNITTEST || m == RUNMODE_UNIX_SOCKET;
1102+
}
1103+
1104+
inline uint64_t PcapPacketCntGet(const Packet *p)
1105+
{
1106+
if (PcapPacketCntRunmodeCanAccess() && p != NULL) {
1107+
return p->pcap_v.pcap_cnt;
1108+
}
1109+
return 0;
1110+
}
1111+
1112+
inline void PcapPacketCntSet(Packet *p, uint64_t pcap_cnt)
1113+
{
1114+
if (PcapPacketCntRunmodeCanAccess() && p != NULL) {
1115+
p->pcap_v.pcap_cnt = pcap_cnt;
1116+
}
1117+
}
1118+
10981119
/**
10991120
* @}
11001121
*/

src/decode.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -622,10 +622,6 @@ typedef struct Packet_
622622
struct Host_ *host_src;
623623
struct Host_ *host_dst;
624624

625-
/** packet number in the pcap file, matches wireshark */
626-
uint64_t pcap_cnt;
627-
628-
629625
/* engine events */
630626
PacketEngineEvents events;
631627

@@ -1522,12 +1518,7 @@ static inline bool DecodeNetworkLayer(ThreadVars *tv, DecodeThreadVars *dtv,
15221518
return true;
15231519
}
15241520

1525-
// temporary macro to get pcap packet count to reduce the number of changes
1526-
// in the follow-up commit
1527-
#define PcapPacketCntGet(p) (p)->pcap_cnt
1528-
#define PcapPacketCntSet(p, cnt) \
1529-
do { \
1530-
(p)->pcap_cnt = (cnt); \
1531-
} while (0)
1521+
uint64_t PcapPacketCntGet(const Packet *p);
1522+
void PcapPacketCntSet(Packet *p, uint64_t pcap_cnt);
15321523

15331524
#endif /* SURICATA_DECODE_H */

src/source-pcap-file-helper.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void PcapFileCallbackLoop(char *user, struct pcap_pkthdr *h, u_char *pkt)
8080
p->ts = SCTIME_FROM_TIMEVAL_UNTRUSTED(&h->ts);
8181
SCLogDebug("p->ts.tv_sec %" PRIuMAX "", (uintmax_t)SCTIME_SECS(p->ts));
8282
p->datalink = ptv->datalink;
83-
p->pcap_cnt = ++pcap_g.cnt;
83+
p->pcap_v.pcap_cnt = ++pcap_g.cnt;
8484

8585
p->pcap_v.tenant_id = ptv->shared->tenant_id;
8686
ptv->shared->pkts++;
@@ -96,8 +96,8 @@ void PcapFileCallbackLoop(char *user, struct pcap_pkthdr *h, u_char *pkt)
9696
if (pcap_g.checksum_mode == CHECKSUM_VALIDATION_DISABLE) {
9797
p->flags |= PKT_IGNORE_CHECKSUM;
9898
} else if (pcap_g.checksum_mode == CHECKSUM_VALIDATION_AUTO) {
99-
if (ChecksumAutoModeCheck(ptv->shared->pkts, p->pcap_cnt,
100-
SC_ATOMIC_GET(pcap_g.invalid_checksums))) {
99+
if (ChecksumAutoModeCheck(ptv->shared->pkts, p->pcap_v.pcap_cnt,
100+
SC_ATOMIC_GET(pcap_g.invalid_checksums))) {
101101
pcap_g.checksum_mode = CHECKSUM_VALIDATION_DISABLE;
102102
p->flags |= PKT_IGNORE_CHECKSUM;
103103
}

src/source-pcap.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ void PcapTranslateIPToDevice(char *pcap_dev, size_t len);
3232
#define LIBPCAP_PROMISC 1
3333

3434
/* per packet Pcap vars */
35-
typedef struct PcapPacketVars_
36-
{
35+
typedef struct PcapPacketVars_ {
36+
uint64_t pcap_cnt;
3737
uint32_t tenant_id;
3838
} PcapPacketVars;
3939

0 commit comments

Comments
 (0)