Skip to content

Commit 0a9b31a

Browse files
CopilotkYroL01
andcommitted
Add -t 0 option to send pcap with current timestamp
Co-authored-by: kYroL01 <[email protected]>
1 parent 481b20c commit 0a9b31a

4 files changed

Lines changed: 22 additions & 11 deletions

File tree

_codeql_detected_source_root

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.

include/captagent/globals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ extern char *backup_dir;
5050
extern char *global_node_name;
5151
extern int timestart;
5252
extern int serial;
53+
extern int use_current_timestamp;
5354
extern const char *captagent_config;
5455

5556
extern struct capture_list main_ct;

src/captagent.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ char *backup_dir;
7474
char *pid_file = NULL;
7575
int timestart;
7676
int serial;
77+
int use_current_timestamp = 0;
7778
const char *captagent_config;
7879
struct capture_list main_ct;
7980
struct action *clist[20];
@@ -127,6 +128,7 @@ void usage(int8_t e)
127128
" -n enable foreground mode\n"
128129
" -f [/path/to/rtpagent.xml] to specify a config file\n"
129130
" -D [/path/to/file.pcap] to specify a pcap file as input\n"
131+
" -t [0] use current timestamp instead of pcap packet timestamp (0 = current time)\n"
130132
" -x [1 - 10] set debug level\n");
131133
exit(e);
132134
}
@@ -280,7 +282,7 @@ int main(int argc, char *argv[])
280282

281283
captagent_config = DEFAULT_CAPT_CONFIG;
282284

283-
while ((c = getopt(argc, argv, "adcvhnEKf:D:x:")) != EOF) {
285+
while ((c = getopt(argc, argv, "adcvhnEKf:D:t:x:")) != EOF) {
284286

285287
switch (c) {
286288
case 'v':
@@ -303,6 +305,11 @@ int main(int argc, char *argv[])
303305
case 'D':
304306
usefile = optarg;
305307
break;
308+
case 't':
309+
if (atoi(optarg) == 0) {
310+
use_current_timestamp = 1;
311+
}
312+
break;
306313
case 'E':
307314
errout = 0;
308315
break;

src/modules/socket/pcap/socket_pcap.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ void callback_proto(unsigned char *arg, struct pcap_pkthdr *pkthdr, unsigned cha
323323
uint8_t ip_proto = 0, erspan_offset = 0;
324324
uint8_t tmp_ip_proto = 0, tmp_ip_len = 0, is_only_gre = 0;
325325

326+
time_t now = use_current_timestamp ? time(NULL) : 0;
327+
326328
unsigned char* ethaddr = NULL;
327329
unsigned char* mplsaddr = NULL;
328330

@@ -449,8 +451,8 @@ void callback_proto(unsigned char *arg, struct pcap_pkthdr *pkthdr, unsigned cha
449451
_msg.rcinfo.ip_family = DLT_MTP2;
450452
_msg.rcinfo.ip_proto = DLT_MTP2;
451453

452-
_msg.rcinfo.time_sec = pkthdr->ts.tv_sec;
453-
_msg.rcinfo.time_usec = pkthdr->ts.tv_usec;
454+
_msg.rcinfo.time_sec = use_current_timestamp ? now : pkthdr->ts.tv_sec;
455+
_msg.rcinfo.time_usec = use_current_timestamp ? 0 : pkthdr->ts.tv_usec;
454456
_msg.tcpflag = 0;
455457
_msg.parse_it = 1;
456458

@@ -734,8 +736,8 @@ void callback_proto(unsigned char *arg, struct pcap_pkthdr *pkthdr, unsigned cha
734736
_msg.rcinfo.dst_port = ntohs(tcp_pkt->th_dport);
735737
_msg.rcinfo.ip_family = ip_ver == 4 ? AF_INET : AF_INET6;
736738
_msg.rcinfo.ip_proto = ip_proto;
737-
_msg.rcinfo.time_sec = pkthdr->ts.tv_sec;
738-
_msg.rcinfo.time_usec = pkthdr->ts.tv_usec;
739+
_msg.rcinfo.time_sec = use_current_timestamp ? now : pkthdr->ts.tv_sec;
740+
_msg.rcinfo.time_usec = use_current_timestamp ? 0 : pkthdr->ts.tv_usec;
739741
_msg.tcpflag = tcp_pkt->th_flags;
740742
_msg.parse_it = 1;
741743

@@ -800,8 +802,8 @@ void callback_proto(unsigned char *arg, struct pcap_pkthdr *pkthdr, unsigned cha
800802
_msg.rcinfo.dst_port = ntohs(tcp_pkt->th_dport);
801803
_msg.rcinfo.ip_family = ip_ver == 4 ? AF_INET : AF_INET6;
802804
_msg.rcinfo.ip_proto = ip_proto;
803-
_msg.rcinfo.time_sec = pkthdr->ts.tv_sec;
804-
_msg.rcinfo.time_usec = pkthdr->ts.tv_usec;
805+
_msg.rcinfo.time_sec = use_current_timestamp ? now : pkthdr->ts.tv_sec;
806+
_msg.rcinfo.time_usec = use_current_timestamp ? 0 : pkthdr->ts.tv_usec;
805807
_msg.tcpflag = tcp_pkt->th_flags;
806808
_msg.parse_it = 1;
807809

@@ -853,8 +855,8 @@ void callback_proto(unsigned char *arg, struct pcap_pkthdr *pkthdr, unsigned cha
853855
_msg.rcinfo.dst_mac = mac_dst;
854856
_msg.rcinfo.ip_family = ip_ver == 4 ? AF_INET : AF_INET6;
855857
_msg.rcinfo.ip_proto = ip_proto;
856-
_msg.rcinfo.time_sec = pkthdr->ts.tv_sec;
857-
_msg.rcinfo.time_usec = pkthdr->ts.tv_usec;
858+
_msg.rcinfo.time_sec = use_current_timestamp ? now : pkthdr->ts.tv_sec;
859+
_msg.rcinfo.time_usec = use_current_timestamp ? 0 : pkthdr->ts.tv_usec;
858860
_msg.tcpflag = 0;
859861
_msg.parse_it = 1;
860862

@@ -916,8 +918,8 @@ void callback_proto(unsigned char *arg, struct pcap_pkthdr *pkthdr, unsigned cha
916918
_msg.rcinfo.dst_mac = mac_dst;
917919
_msg.rcinfo.ip_family = ip_ver == 4 ? AF_INET : AF_INET6;
918920
_msg.rcinfo.ip_proto = ip_proto;
919-
_msg.rcinfo.time_sec = pkthdr->ts.tv_sec;
920-
_msg.rcinfo.time_usec = pkthdr->ts.tv_usec;
921+
_msg.rcinfo.time_sec = use_current_timestamp ? now : pkthdr->ts.tv_sec;
922+
_msg.rcinfo.time_usec = use_current_timestamp ? 0 : pkthdr->ts.tv_usec;
921923
_msg.tcpflag = 0;
922924
_msg.parse_it = 1;
923925

0 commit comments

Comments
 (0)